Obtener elementos seleccionados y ninguno seleccionado

I want to find out what items are Selected and anything that is not selected become 0

This is useful for the listbox (or dropdown), anything that is highlighted it mean it has been added (LEFT JOIN)

I could not get the SQL to work:

SELECT StoreID,  ft.id, name, count(1) as Selected 
FROM `store_food_type` AS sft  
    LEFT JOIN food_type AS ft 
        ON ft.id = sft.food_type_id 
WHERE StoreID = 17633
GROUP BY ft.id

UNION

SELECT 0,  ft.id, name, 0 as Selected FROM food_type AS ft

Edit: It did not produce an error, it show the result but name are duplicated which I dont want.

It appear like this:

+------------+------+----------+----------+
| StoreID    | id   | name     | Selected |
+------------+------+----------+----------+
|      17633 |    1 | Indian   |        1 |
|      17633 |    8 | American |        1 |
|          0 |    1 | Indian   |        0 |
|          0 |    2 | English  |        0 |
|          0 |   25 | Kurdish  |        0 |
|          0 |    5 | Chinese  |        0 |
|          0 |    7 | Turkish  |        0 |
|          0 |    8 | American |        0 |
+------------+------+----------+----------+

I want the result to appear like this:

+------------+------+----------+----------+
| StoreID    | id   | name     | Selected |
+------------+------+----------+----------+
|      17633 |    1 | Indian   |        1 |
|      17633 |    8 | American |        1 |
|          0 |   25 | Kurdish  |        0 |
|          0 |    5 | Chinese  |        0 |
|          0 |    7 | Turkish  |        0 |
|          0 |    8 | American |        0 |
+------------+------+----------+----------+

preguntado el 09 de marzo de 12 a las 16:03

In what way does this query fail to work? Does it produce an error, does it run without errors but select no rows or does it return the wrong rows? -

@MarkBannister I have updated my question. -

Can a food be selected multiple times by the same store? -

3 Respuestas

Your rows are getting duplicated because of the UNION in your query. In fact, your entire query is backwards - You're trying to get a list of all food types, with an indicator if they're listed for the given store.

Prueba esto en su lugar:

SELECT ft.id, ft.name, COALESCE(sft.storeId, 0) as storeId, 
       CASE WHEN sft.storeId IS NOT NULL THEN '1'
            ELSE '0' END as selected
FROM food_type a ft
LEFT JOIN store_food_type as sft
ON sft.food_type_id = ft.id
AND sft.storeId = 17633

Although you don't strictly need the selected column, as you could simply test whether result column storeId was 0 or not.

respondido 09 mar '12, 17:03

Probably the simplest approach would be to group the results from your unioned query - like so:

SELECT MAX(StoreID) StoreID,  id, MAX(name) name, MAX(Selected) Selected FROM
(SELECT StoreID,  ft.id, name, count(1) as Selected 
 FROM `store_food_type` AS sft  
     LEFT JOIN food_type AS ft 
         ON ft.id = sft.food_type_id 
 WHERE StoreID = 17633
 GROUP BY ft.id
 UNION
 SELECT 0,  ft.id, name, 0 as Selected FROM food_type AS ft) sq
GROUP BY id

respondido 09 mar '12, 17:03

I'm not sure this answer is any better than the UNION option offered by Mark Bannister, but it is another way to do it.

SELECT sft.storeid, ft.id, ft.name, count(sft.storeid) selected
FROM   store_food_type sft
       FULL JOIN food_type ft ON ft.id = sft.food_type_id
WHERE  sft.storeid = 17633 or sft.storeid is null
GROUP  BY ft.id, ft.name, sft.storeid

If a food item can only be selected once for any given store, then the "selected" field and the GROUP BY are redundant.

respondido 09 mar '12, 18:03

I think MySQL do not support 'FULL JOIN' - Vuelvo enseguida

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.