有没有更好的方法来编写这个MySQL查询?
16lz
2021-01-22
Like the question says is there a better way to write this:
就像问题所说有更好的方法来写这个:
SELECT clients_lists.*,
COUNT(clients_lists_relationships.clientid)
FROM clients_lists
LEFT JOIN clients_lists_relationships
ON clients_lists.listid = clients_lists_relationships.listid
WHERE clients_lists.parentid = 1
GROUP BY clients_lists.listid;
1 个解决方案
#1
3
Not really. The query looks fine to me. You can try to use aliases for better readability:
不是真的。查询看起来很好。您可以尝试使用别名以获得更好的可读性:
SELECT c.*,
COUNT(r.clientid)
FROM clients_lists c
LEFT JOIN clients_lists_relationships r
ON c.listid = r.listid
WHERE c.parentid = 1
GROUP BY c.listid;
But it won't really make a technical difference (not that I know).
但它不会真正产生技术差异(不是我所知道的)。
Also make sure that both clients_lists.listid
and clients_lists_relationships.listid
are of the same datatype and length and are indexed. This will definitely help your query's performance.
还要确保clients_lists.listid和clients_lists_relationships.listid具有相同的数据类型和长度并且已编制索引。这肯定有助于查询的性能。
更多相关文章
- Mysql Where使用列别名
- MySQL数据库表名、列名、别名区分大小写的问题
- 在Delete From语句中带有别名的表变量。
- 在SQL SELECT语句中重用别名字段
- 每个派生表必须有自己的别名(sql&php)[重复]
- SQL文结果集起别名