我的数据库中有三个表。
Table A
包含字段
KEYID | KeyName
27 | Income
28 | Account Number
Table B
包含字段
UserID | Email | Name | Phone
481 | test@gmail.com | test | 99999999
Table C
包含字段
ID | KEYID | UserID | Value
1 | 27 | 481 | 10,000
我需要显示表字段标题:
UserID | Email | Name | Phone | Income
并且表值应该是这样的:
481 | test@gmail.com | test | 99999999 | 10,000
我可以获得应该在表格中显示的KeyID。在此示例中,KeyIDs字符串是' 27' 。我尝试加入,我可以取得&显示表中的值。但我不知道我怎么能把关键名称显示为表头。
任何想法。?
答案 0 :(得分:1)
您可以使用一对内连接
select b.UserID, b.Email , b.Name, c.value as income
from tableB as b inner join tableC as C on b.userID = c.userId
inner join tableA as a on a.keyID = c.keyID
and a.keyname = 'Income';
以及您在评论中提供的查询
select
b.UserID
, b.Email
, b.Name
, Group_Concat(Distinct Concat(c.keyID,’^:^’,c.value)
Order By c.id Separator ‘;’) As Keyvalues
from tableB as b
inner join tableC as C on b.userID = c.userId
inner join tableA as a on a.keyID = c.keyID;
和CASE应该是
select
b.UserID
, b.Email
, b.Name
, Group_Concat(Distinct CASE
WHEN c.keyID IN ('1,23,10') THEN Concat(c.keyID,’^:^’,c.value) END
Order By c.id Separator ‘;’) As Keyvalues
from tableB as b
inner join tableC as C on b.userID = c.userId
inner join tableA as a on a.keyID = c.keyID;
答案 1 :(得分:0)
此查询应有助于获得您的愿望结果。
select b.UserID, b.Email, b.Name, b.Phone, c.Value as Income
from table_b as b
JOIN table_c as c ON (b.UserID = c.UserID)
where c.KEYID = 27
答案 2 :(得分:0)
试试这个:
SELECT b.userid, b.email, b.name, b.phone, c.value as income
FROM a
LEFT JOIN c on c.keyid = a.keyid
LEFT JOIN b ob b.userid = c.userid