gpt4 book ai didi

mysql - 返回 mysql 所在的所有行

转载 作者:行者123 更新时间:2023-11-29 10:36:06 25 4
gpt4 key购买 nike

我有这个表:

tbl_1
+---+--------+
|ID | NAME |
+---+--------+
| 1 | Name_1 |
| 2 | Name_2 |
| 3 | Name_3 |
+---+--------+

tbl_2
+---+----------+---------+
|ID | MENTIUNI | ID_NAME |
+---+----------+---------+
| 1 | abc | 1 |
| 2 | abcd | 2 |
| 3 | abc | 1 |
| 4 | abcd | 1 |
| 5 | abc | 2 |
| 6 | abcd | 3 |
+---+----------+---------+

SELECT
tbl_1.NAME,
count(tbl_2.MENTIUNI) AS NR_MENTIUNI

FROM tbl_1

LEFT JOIN tbl_2 ON tbl_1
tbl_1.ID = tbl_2.ID_NAME

WHERE tbl_2.MENTIUNI = 'abc'

查询 ul de mai sus imi selecteaza din tbl_1.ID,numai cele care se regasesc in tbl_2 sub conditia tbl_2.MENTIUNI = 'abc'

Cum pot afisa toate id-urile din tbl_1 chiar daca am conditie 在哪里?

上面的查询从 tbl_1.ID 中选择我,仅在 tbl_2.MENTIUNI = 'abc' 条件下在 tbl_2 中找到的内容

即使我在 where 中有条件,如何显示 tbl_1 中的所有 id?

+------+-------------+
| NAME | NR_MENTIUNI |
+------+-------------+
|Name_1| 2 |
|Name_2| 1 |
|Name_3| 0 |
+------+-------------+

谢谢!

最佳答案

将条件从整个查询移至 JOIN 子句,因此它仅过滤要连接的第二个表中的记录。像这样的事情:

SELECT
tbl_1.NAME,
count(tbl_2.MENTIUNI) AS NR_MENTIUNI
FROM tbl_1
LEFT JOIN tbl_2
ON tbl_1.ID = tbl_2.ID_NAME
AND tbl_2.MENTIUNI = 'abc'

关于mysql - 返回 mysql 所在的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com