gpt4 book ai didi

php - 选择连接重复

转载 作者:行者123 更新时间:2023-11-29 11:12:07 26 4
gpt4 key购买 nike

我的数据库有 3 个表,例如:

服务

   id | name | service_id (UNIQUE)    1  | new  |    123    2  | service1  |    543

counts

   id | counts | counts_price | service_id    1  | 100    |     200      | 123    5  | 200    |     250      | 123    6  | 300    |     350      | 543

background

   id | background | background_price | service_id    1  |   white    |        50        | 123   12  |   red      |        150        | 123    

My query looks like:

SELECT background.background, services.name, counts.count 
FROM services INNER JOIN background
ON background.service_id=services.service_id INNER JOIN counts
ON counts.service_id=services.service_id
WHERE services.service_id = :service_id

:service_id = 123
在输出中我得到
var_dump

为什么会重复?查询出了什么问题?

<pre>
array (size=4)
0 =>
array (size=3)
0 => string 'white' (length=5)
1 => string 'new' (length=3)
2 => string '200' (length=3)
1 =>
array (size=3)
0 => string 'red' (length=3)
1 => string 'new' (length=3)
2 => string '200' (length=3)
2 =>
array (size=3)
0 => string 'white' (length=5)
1 => string 'new' (length=3)
2 => string '250' (length=3)
3 =>
array (size=3)
0 => string 'red' (length=3)
1 => string 'new' (length=3)
2 => string '250' (length=3)
</pre>

最佳答案

为此添加分组依据

GROUP BY services.service_id

所以你的最终查询将是:

SELECT background.background, services.name, counts.count, services.service_id 
FROM services INNER JOIN background
ON background.service_id=services.service_id INNER JOIN counts
ON counts.service_id=services.service_id
WHERE services.service_id = :service_id
GROUP BY services.service_id

关于php - 选择连接重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40374988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com