duanqiao1961 2018-05-23 15:28
浏览 102
已采纳

SQL查询 - 在一个结果行中显示连接结果[重复]

This question already has an answer here:

I want to create an advanced search based on one table and other tables my tables:

estate :
    +------+-------------+-------------------+-------------------+
    | id   |     title   |       cat_id      |  date              |
    +------+-------------+-------------------+-------------------+
    |  1   |    test1    |         1         |      1526793203   |
    |  2   |    test2    |         2         |      1526793203   |
    |  3   |    test3    |         3         |      1526793203   |
    +------+--------------+------------------+-------------------+

estate_risk
    +------+-------------+----------------+--------------+
    | id   | estate_id   | title           | consequence |
    +------+-------------+----------------+--------------+
    |  3   |    1        |  risktitle1    | 123          |
    |  4   |    1        |  risktitle2    | 433          |
    |  5   |    1        |  risktitle3    | 523          |
    |  6   |    2        |  risktitle4    | 976          |
    |  7   |    2        |  risktitle5    | 422          |
    |  8   |    3        |  risktitle6    | 124          |
    +------+-------------+----------------+--------------+
related_estate 
    +------+-------------+----------------+--------------+
    | id   | estate_id   | title           | storage      |
    +------+-------------+----------------+--------------+
    |  3   |    1        |  testdata      | 1             |
    |  4   |    1        |  testdata2     | 2             |
    |  5   |    1        |  testdata3     | 3             |
    |  6   |    2        |  testdata4     | 4             |
    |  7   |    2        |  testdata5     | 5             |
    |  8   |    5        |  testdata6     | 6             |
    +------+-------------+----------------+---------------+

And some other tables...(foreign all tables is estate_id) I want to get all the data in a row,in other words for example, I have several related data in the estate_risk table and when I use join query get this result data :

sample query one join:

SELECT R.id,R.title,C.title,C.storage FROM estate R LEFT JOIN estate_risk as C ON estate_risk .estate_id = R.id

result :

----------+------------------+----------------+--------------+
   R.id   |      R.title     |   C.title      |  C.consequence  
----------+------------------+----------------+--------------+
    1     |         test1    |   risktitle1   |  123         |
    1     |         test1    |   risktitle2   |  433         |
    1     |         test1    |   risktitle3   |  523         |
    2     |         test2    |   risktitle4   |  976         |
    2     |         test2    |   risktitle5   |  422         |
    3     |         test3    |   risktitle6   |  124         |
----------+------------------+----------------+--------------+

Everything is right but i want get all the data in a row that's mean only a row R.id with all C.title in one row My main goal is to display the page search results just one estate with all other data tables

+------+-------------+----------------+----------------+
  id   | estate_title| estate_risk    | related_estate |
+------+-------------+----------------+----------------+
  1        test1          risktitle1       testdata1  
                          risktitle2       testdata2      
                          risktitle3       testdata3      
---------------------------------------------------------
2 ...

Maybe my goal is not right, but I do not know how I can get this output And this should be done on the database side, or programming side ? What ideas can be made on the programming side?

</div>
  • 写回答

1条回答 默认 最新

  • dsjswclzh40259075 2018-05-23 15:43
    关注

    It looks like what you want is a GROUP_CONCAT to put together the risktitles and related_estates.

    GROUP_CONCAT(C.title SEPARATOR '
    '),
    GROUP_CONCAT(Some_other_column SEPARATOR '
    ')
    

    Once you have your group functions, I believe grouping by R.id as Yanet suggested and the data should come together correctly.

    If you do have larger data sets though, there are limitations to MySQL's concatenation functions. So in that case you might need to return the data as you are already and then reformat it to match what you want in the PHP side.

    Group Concat Limit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路