doucang8303 2016-07-19 10:57
浏览 16
已采纳

在奇怪的要求中加入mysql中的三个表

I have three tables in my db.

Table A has the fields

KEYID | KeyName
27    | Income
28    | Account Number

Table B has the fields

UserID | Email          | Name | Phone
481    | test@gmail.com | test | 99999999

Table C has the fields

ID | KEYID | UserID | Value
1  |   27  |   481  | 10,000

I need to display the table fields headers are:

UserID | Email          | Name |   Phone  | Income

and the table values should be like this:

 481   | test@gmail.com | test | 99999999 | 10,000

I can get the KeyIDs which should be displayed in the table. In this example the KeyIDs string is '27' . I tried with joining and i can fetch & display the value in the table. but i dont know how i can show the key name as table header.

Any Idea.?

  • 写回答

3条回答 默认 最新

  • doubei5310 2016-07-19 11:06
    关注

    You can use a pair of inner join

    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';     
    

    and the query you provided in comment

    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;  
    

    and with CASE should be

     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; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)