[英]php mySQL group concat and group by
I have two table我有两张桌子
table 1:
row | car_id | car_model |
1 1 CAR 1
2 2 CAR 2
3 3 CAR 3
4 4 CAR 4
table 2:
row | car_id | car_features |
1 1 Features 1
2 2 Features 2
3 2 Features 3
What I want to display :我想显示的内容:
row | car_id | car_model | car_features |
1 1 CAR 1 Features 1
2 2 CAR 2 Features 2,Features 3
3 3 CAR 3
4 4 CAR 4
if I using group concat with group by如果我使用 group concat 和 group by
there will display row 1,2 and I want to display all the rows...将显示第 1,2 行,我想显示所有行...
row | car_id | car_model | car_features |
1 1 CAR 1 Features 1
2 2 CAR 2 Features 2,Features 3
Please send me the sql statement..THANK!!请把sql语句发给我..谢谢!!
this is my query这是我的查询
$format = mysql_query("SELECT c.* , p.*, d.*,f.* ,GROUP_CONCAT(DISTINCT e.features_title SEPARATOR '<br>') AS 'car_features' FROM bsi_car_master c,bsi_car_type p, bsi_car_vendor d, bsi_selected_features f, bsi_car_features e WHERE c.car_type_id=p.id AND c.car_vendor_id=d.id AND c.car_id = f.car_id AND f.features_id = e.id GROUP BY c.car_id");
You should use an left join as suggested in the comments:您应该按照评论中的建议使用左连接:
SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.car_id=table2.car_id;
Take a look at some MySQL tutorials.看看一些 MySQL 教程。 w3schools.org helped me a lot.
w3schools.org 帮了我很多。
I guess that should do the trick for you.我想这应该对你有用。
But as said, please put a little more effort in your questions.但正如所说,请在您的问题上多花些力气。 I am glad to help, StackOverflow is not a free code service!
很高兴为您提供帮助,StackOverflow 不是免费的代码服务!
Use an outer join, so as to get rows even when no matching record in table2 exists.使用外连接,以便即使在 table2 中不存在匹配记录时也能获取行。
UPDATE: Now that you have showed your query:更新:现在您已经显示了您的查询:
It is very strange to see your query, because on one hand you seem to be a beginner, on the other you are using a join syntax that was taught last century and is long since out-dated.看到您的查询很奇怪,因为一方面您似乎是初学者,另一方面您正在使用上个世纪教授并且早已过时的连接语法。
So your query should better look like this:所以你的查询最好看起来像这样:
SELECT
c.*,
p.*,
d.*,
f.*,
GROUP_CONCAT(DISTINCT e.features_title SEPARATOR '<br>') AS car_features
FROM bsi_car_master c
INNER JOIN bsi_car_type p ON p.id = c.car_type_id
INNER JOIN bsi_car_vendor d ON d.id = c.car_vendor_id
LEFT JOIN bsi_selected_features f ON f.car_id = c.car_id
LEFT JOIN bsi_car_features e ON e.id = f.features_id
GROUP BY c.car_id
p and d are inner-joined, ie there must be a match in p and d otherwise the c record is not selected. p 和 d 是内连接的,即 p 和 d 中必须有匹配项,否则不会选择 c 记录。 But f is outer-joined, so you get c (and p and d) even when no record in f exists for that c (then all f columns are null in that record).
但是 f 是外连接的,因此即使 f 中不存在该 c 的记录(然后该记录中的所有 f 列都为空),您也会得到 c(以及 p 和 d)。 As to selecting e, we must also outer-join this, because again, if we got no match, we still want the record c with p and d.
至于选择e,我们还必须对其进行外连接,因为同样,如果我们没有匹配,我们仍然需要带有p和d的记录c。
I changed 'car_features'
to car_features
by the way. 'car_features'
car_features
,我将'car_features'
更改为'car_features'
。 Single quotes are for string literals.单引号用于字符串文字。 Double quotes are for names, but they are not needed here
双引号用于名称,但此处不需要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.