Domanda

I have those 2 tables

id_of_orders

id_order| time              | username | price
35      |2013-08-11 15:03:01|          |0.00
36      |2013-08-21 12:43:31|skata     |0.00
37      |2013-08-21 12:43:55|skata     |12.00

orders

order_id  | product |   ulika   
35        |frento   |zaxari,fountoukia,troufa
35        |frento   |zaxari,kakao
35        |frento   |
36        |frapes   |zaxari
37        |frento   |
35        |frapes   |

I want for a specific product to return all the record's data that contain it I use GROUP_CONCAT but it does not return all the values of the product.

if(!empty($_SESSION['proions'])){
    $query= "SELECT i.id_order, i.time, i.username, i.price,  GROUP_CONCAT(o.product) as PRO
    FROM id_of_orders i INNER JOIN orders o
    ON i.id_order = o.order_id
    WHERE o.product = '$_SESSION[proions]'";
    $query .=" GROUP BY i.id_order" ;
    $result = mysql_query($query);
}

For example if i try to collect the record's data that contains 'frento' i get those results . I miss the product 'frapes' .

35: -> 2013-08-11 15:03:01 0.00 frento,frento,frento
37: -> 2013-08-21 12:43:55 skata 12.00 frento
È stato utile?

Soluzione

Your query is filtering all the rows just on the product. You need to move that comparison to a having clause:

SELECT i.id_order, i.time, i.username, i.price, GROUP_CONCAT(o.product) as PRO
FROM id_of_orders i INNER JOIN
     orders o
     ON i.id_order = o.order_id
GROUP BY i.id_order
HAVING max(o.product = '$_SESSION[proions]'") > 0;

This will return all products for an order where at least one of the rows matches the product you want.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top