在 MySQL 中选择查询

发布于 2024-09-29 06:50:08 字数 674 浏览 3 评论 0原文

两个表

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒江雪… 2024-10-06 06:50:08

试一试:

select SUM(isDeleted)
from Table2 
where Table1_ID in (
  select ID from Table1
  where FileName = (select FileName
                    from Table1
                    where ID = 1)
)

编辑:获取文件计数:

select count(*) 
from Table1 a
join Table2 b on a.ID = b.Table1_ID and b.isDeleted = 1
where a.FileName = (select FileName
                   from Table1
                   where ID = 1)

Give this a shot:

select SUM(isDeleted)
from Table2 
where Table1_ID in (
  select ID from Table1
  where FileName = (select FileName
                    from Table1
                    where ID = 1)
)

Edit: to get file count:

select count(*) 
from Table1 a
join Table2 b on a.ID = b.Table1_ID and b.isDeleted = 1
where a.FileName = (select FileName
                   from Table1
                   where ID = 1)
红ご颜醉 2024-10-06 06:50:08

这对我有用:

declare @id int
set @id = 1 /*Or 2 or 3 or 4, etc.*/

select sum(isdeleted)
from table2
where table1_id in 
    (select id 
    from table1 
    where filename = (select filename 
                      from table1 
                      where id = @id))

编辑:我看不出这与 Fosco 的答案有什么不同。

This works for me:

declare @id int
set @id = 1 /*Or 2 or 3 or 4, etc.*/

select sum(isdeleted)
from table2
where table1_id in 
    (select id 
    from table1 
    where filename = (select filename 
                      from table1 
                      where id = @id))

Edit: I can't see how this is different from Fosco's answer.

冰雪梦之恋 2024-10-06 06:50:08
SELECT COUNT(t1.FileName) FROM Table1 t1 
INNER JOIN table2 t2 ON t2.Table1_ID =t1.ID AND t2.isDeleted=1
WHERE t1.ID=X; 
SELECT COUNT(t1.FileName) FROM Table1 t1 
INNER JOIN table2 t2 ON t2.Table1_ID =t1.ID AND t2.isDeleted=1
WHERE t1.ID=X; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文