我尝试使用uid获取first_name
和last_name
,并从FILE_ATTACH_INFO表中获取url
使用相同的uid。我在下面尝试但看起来它不起作用,因为它们有不同数量的列。我怎样才能做到这一点?
SELECT first_name, last_name
FROM `USER` AS a
WHERE uid = '00283366-85df-4c1b-9d94-7beafb8d44fd'
UNION
SELECT url
FROM `FILE_ATTACH_INFO`
WHERE user_id = '00283366-85df-4c1b-9d94-7beafb8d44fd'
我希望得到
first_name | last_name | url
------------------------------------------------
kim | asdf | https://qwef.com
答案 0 :(得分:0)
Select * FROM `USER` a JOIN `FILE_ATTACH_INFO` b on a.uid = b.user_id WHERE a.uid = '00283366-85df-4c1b-9d94-7beafb8d44fd'
尝试内部联接...
答案 1 :(得分:0)
SELECT a.first_name, a.last_name, b.url
FROM USER AS a
LEFT OUTER JOIN FILE_ATTACH_INFO b ON a.uid = b.user_id
WHERE a.uid = '00283366-85df-4c1b-9d94-7beafb8d44fd'
答案 2 :(得分:0)
因为您使用了错误的操作,所以只有在两个表的列相同时才能应用union。在这种情况下,您需要在它们共享的列上连接两个表。
SELECT u.first_name, u.last_name, fa.url FROM
user u
INNER JOIN file_attach_info fa
on u.uid = fa.user_id
答案 3 :(得分:0)
尝试这个
SELECT a.first_name,a.last_name,f.url FROM USER AS a INNER JOIN FILE_ATTACH_INFO AS f ON f.user_id=a.uid WHERE 1;