简体   繁体   中英

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

there will display row 1,2 and I want to display all the rows...

     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!!

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. w3schools.org helped me a lot.

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!

Use an outer join, so as to get rows even when no matching record in table2 exists.

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. 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). 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.

I changed 'car_features' to car_features by the way. Single quotes are for string literals. Double quotes are for names, but they are not needed here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM