在 MySQL 中选择查询
两个表
Table1
ID FileName
1 abc
2 abc
3 abc
4 xyz
Table2
ID Table1_ID isDeleted
1 1 1
2 2 1
3 3 0
4 4 0
我需要通过传递 table1 的任何 ID 来获取 isDeleted=1 的文件名计数,即对于 ID 的所有值(1,2,3),我需要计数为 2
我尝试了以下操作查询
SELECT COUNT(t1.FileName) FROM Table1 t1
LEFT OUTER JOIN Table1 t11 ON t1.FileName=t11.FileName
INNER JOIN table2 t2 ON t2.Table1_ID =t1.ID AND t2.isDeleted=1
WHERE t1.ID=X;
X-1,2,3
始终返回 3。
编辑: 我需要通过传递第一个表中的 ID 来获取第一个表中文件名的计数。计数应基于第二个表中的 isdeleted 列。这些表通过列 ID (table1) 和 Table1_ID (table2) 关联
Two tables
Table1
ID FileName
1 abc
2 abc
3 abc
4 xyz
Table2
ID Table1_ID isDeleted
1 1 1
2 2 1
3 3 0
4 4 0
I need to get the count of filename for the isDeleted=1 by passing any ID of table1, i.e for all the values(1,2,3) of ID, i need the count as 2
I tried with the following query
SELECT COUNT(t1.FileName) FROM Table1 t1
LEFT OUTER JOIN Table1 t11 ON t1.FileName=t11.FileName
INNER JOIN table2 t2 ON t2.Table1_ID =t1.ID AND t2.isDeleted=1
WHERE t1.ID=X;
X-1,2,3
This always returns 3.
Edit: I need to get the count of the filename from the first table by passing the ID from the first table. The count should be based on the isdeleted column in second table. The tables are related by the column ID (table1) and Table1_ID (table2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试一试:
编辑:获取文件计数:
Give this a shot:
Edit: to get file count:
这对我有用:
编辑:我看不出这与 Fosco 的答案有什么不同。
This works for me:
Edit: I can't see how this is different from Fosco's answer.