我想从多个表中选择数据库中的一些行
第一张表:
Table name: ctud
--------------------------------
id + user_id + dep_id
1 1 1
2 2 1
3 3 2
4 4 3
第二张表:
Table name: cdot
-------------------------------
id + username + name
1 Hello Emre
2 Merhaba Emma
3 Aloha Micheal
4 Yup Test
我想从两个表中获取数据,为此,我使用此代码:
select *
FROM ctud,cdot
where ctud.user_id = cdot.username
and ctud.user_id = 1;
但我得到一个空白的屏幕..这可能是什么原因?
答案 0 :(得分:1)
你在这种情况下加入你的桌子
ctud.user_id = cdot.username
但ctud.user_id
例如是1
和cdot.username
Hello
。
这不符合并且不会返回结果。在这些包含相同值的表中需要2列才能建立这些表的连接。
你可能想做
select *
FROM ctud
join cdot on ctud.user_id = cdot.id
where ctud.user_id = 1;
答案 1 :(得分:0)
您只需要此代码
select *
FROM ctud,cdot
where ctud.user_id = cdot.id and ctud.user_id = 1;