How to combine multiple true / false lines in MYSQL

I have a query with a join in a table containing different true / false columns. I can force DISTINCT and GROUP BY to guarantee that only one unique row is returned, however true / false strings do not behave predictably, for example:

**Table 1**
loc_id name
-------------
1      a
2      b
3      c
4      d

**Table 2**
prod_id loc_id value
-------------
1       1      abc
2       1      bcd
3       1      def
4       2      fgh

**Table 3**
prod_id flag
-------------
1       0
2       0
3       1
4       1

SELECT DISTINCT name, flag from table1
LEFT JOIN table3 ON table3.prod_id = table2.prod_id
LEFT JOIN table2.loc_id=table1.loc_id

, loc. , , , , , 0, 1, , 1. 0, 0 ... GROUP BY, , , , 0, 1 , 0?

+3
1

1, 1 0, , MAX ().

SELECT name, MAX(flag) from table1
LEFT JOIN table3 ON table3.prod_id = table2.prod_id
LEFT JOIN table2.loc_id=table1.loc_id
GROUP BY name
+5

Source: https://habr.com/ru/post/1775844/


All Articles