gpt4 book ai didi

MySQL 从第一个表中选择所有内容,并使用第二个表过滤器的特定 where 子句连接第二个表中的匹配数据

转载 作者:行者123 更新时间:2023-11-29 12:43:44 25 4
gpt4 key购买 nike

我在尝试从数据库中提取数据时遇到问题。

这是我的表格设计

我在尝试从数据库中提取数据时遇到问题。

这是我的表格设计

表1:

user_id    username
1 test
2 test2
3 test3

表2:

id         table2_userid   key        value
1 2 position admin
2 2 name myname

我想要输出的是:

user_id     username       key        value
1 test NULL NULL
2 test2 position admin
3 test3 NULL NULL

这是我当前的sql代码:

 SELECT table1.user_id, table1.username, table2.key, table2.value 
FROM table1
LEFT JOIN table2 ON table1.user_id = table2.table2_userid WHERE table2.key="position"

但是,这不会返回任何内容。请帮助我。

谢谢。

最佳答案

尝试以下查询,它将解决您的问题:

SELECT table1.user_id, table1.username, table2.key, table2.value FROM 
table1 LEFT JOIN table2 ON table1.user_id = table2.table2_userid and
table2.key="position" group by table1.user_id

关于MySQL 从第一个表中选择所有内容,并使用第二个表过滤器的特定 where 子句连接第二个表中的匹配数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25741610/

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