How do I GROUP BY and then LEFT JOIN? [duplicate] - mysql

This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 8 years ago.
I have this table:
------------------
| id | foods |
------------------
| 1 | cookies |
| 2 | cake |
| 3 | milk |
------------------
And this table:
------------------
| id | colors |
------------------
| 1 | red |
| 1 | blue |
| 1 | pink |
| 2 | orange |
| 2 | yellow |
| 2 | purple |
| 3 | cyan |
| 3 | gold |
| 3 | silver |
------------------
I want to SELECT the foods from the first table and match them with the colors from the second table, based on id.
So I want the output to look like this:
--------------------------------------------
| id | foods | colors |
--------------------------------------------
| 1 | cookies | red, blue, pink |
| 2 | cake | orange, yellow, purple |
| 3 | milk | cyan, gold, silver |
--------------------------------------------
How can I do this?
EDIT: This is not a duplicate. I am not trying to concatenate. I am trying to merge a concatenated table into a regular table based on ID.

Should look something like this:
SELECT f.food, GROUP_CONCAT(c.color SEPARATOR ' ') FROM food_table f join color_table c on f.food_id = c.id GROUP BY food;
Documentation can be found here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Related

mysql search in left join

I have two tables, A and B
table A has this columns:
id, title
table B has these columns:
id, content, A_id, type
In table B, A_id is foreign key related to table A.
I want to write a search query that search title in A and left Join to B where A.id = B.A_id and search B.content
It is so easy:
SELECT A.*, B.content FROM A LEFT JOIN B ON A.id=B.A_id
WHERE A.title like 'sometitle' AND B.content like 'somecontent';
But I have column 'type' in table B that have entries like this: 'good','bad','ugly','good','good'...
when I search 'content' in table B I want that every content to be related to a 'type'. In other words, the query should give me result related to content1 and type good And content2 and type bad...
Example:
TAble A:
+----+--------+
| id | title |
+----+--------+
| 1 | white |
| 2 | blue |
| 3 | red |
| 4 | white |
| 5 | blue |
+----+--------+
table B:
+----+---------+------+------+
| id | content | A_id | type |
+----+---------+------+------+
| 1 | dog | 1 | good |
| 2 | dog | 1 | bad |
| 3 | cat | 2 | good |
| 4 | cat | 2 | bad |
| 4 | cat | 2 | ugly |
| 6 | crow | 3 | good |
| 7 | crow | 3 | bad |
| 8 | crow | 3 | ugly |
| 9 | mouse | 2 | good |
| 10 | zebra | 3 | bad |
| | | | |
+----+---------+------+------+
I want a query that that its output be this:
+----+-------+---------+------+
| id | title | content | type |
+----+-------+---------+------+
| 1 | white | dog | good |
| 2 | blue | cat | bad |
+----+-------+---------+------+
SQL HERE
Your query for the expected result was almost correct, just you used the wrong column content whether the correct column would be type. Look at the below query :
SELECT A.id, A.title, B.content, B.type FROM A
LEFT JOIN B ON A.id=B.A_id
WHERE (a.title='white' AND B.type='good')
OR (a.title='blue' AND B.type='bad')
ORDER BY a.id asc
OUTPUT
+----+-------+----------+------+
| id | title | content | type |
+----+-------+----------+------+
| 1 | white | content5 | good |
| 3 | blue | content6 | bad |
| 4 | white | content3 | good |
| 5 | blue | content9 | bad |
+----+-------+----------+------+
I think the query you posted at the end of your question should start like this:
SELECT A.id, A.title, B.A_id, B.content, B.type .... (etc.)
so that also B.A_id is fetched

sql many to many relationship with only 2 tables

So I have two tables I need to join for a client. These tables only share 1 field in common (COLOR, and it isn't a unique key ident). Is is possible to join/relate these two tables?
So let's say theoretically I have two tables with COLOR and a COLOR ATTRIBUTE, as follows:
+-------+----------+
| COLOR | NAME |
+-------+----------+
| red | brian |
| red | ben |
| red | tom |
| red | jennifer |
| blue | tom |
| blue | billy |
| blue | michelle |
+-------+----------+
And another table that is ONLY related by the color column, but has multiple color weights:
+-------+--------+
| COLOR | WEIGHT |
+-------+--------+
| red | 12 |
| red | 3 |
| red | 11 |
| blue | 4 |
| blue | 23 |
| blue | 7 |
| blue | 5 |
| blue | 10 |
+-------+--------+
So how can I join these two tables given ONLY the color column is shared? What would the result look like? Thanks in advance!
While color is not a unique identifier, if you plan to perform a join on it, it will be treated as such.
SELECT * FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.COLOR = t2.COLOR
ORDER BY COLOR DESC;
This outputs three columns with 12 "red rows" and 15 "blue rows."

ORDER BY - Identical values

Here is a table structure example:
// tablename
+----+------+---------+
| id | numb | color |
+----+------+---------+
| 1 | 4 | green |
| 2 | 4 | yellow |
| 3 | 3 | red |
+----+------+---------+
Here is a query example:
SELECT id, numb, color FROM tablename ORDER BY numb asc
The result will be:
+----+------+---------+
| id | numb | color |
+----+------+---------+
| 3 | 3 | red |
| 1 | 4 | green |
| 2 | 4 | yellow |
+----+------+---------+
Now, my focus is on the order of these rows:
| 3 | 4 | green |
| 2 | 4 | yellow |
Because their numb values are equal, Now I want to know, for several executing that query, they will be constant? (Is order guaranteed for the identical values?) Or there isn't any guarantee and I should use another column name in the query like this ORDER BY numb, id asc ?
Short answer: No, there is no guarantee. (as #Strawberry wrote under the question)
Full answer: You can add a new column named sort_identical, And fill it whatever you like. And then use this:
... ORDER BY numb, sort_identical asc
(Also you can use id instead of creating a new column - But if you need to sort it differently than id, then create a new column)
+----+------+---------+----------------+
| id | numb | color | sort_identical |
+----+------+---------+----------------+
| 3 | 3 | red | 1 |
| 1 | 4 | green | 2 |
| 2 | 4 | yellow | 3 |
+----+------+---------+----------------+

Simplifying MySQL query - 2 queries into 1

I have a table that looks like this:
+----+--------+-------+
| id | entity | word |
+----+--------+-------+
| 1 | 1 | red |
| 2 | 1 | green |
| 3 | 1 | blue |
| 4 | 2 | car |
| 5 | 2 | truck |
| 6 | 2 | train |
| 7 | 3 | water |
| 8 | 3 | milk |
| 9 | 3 | soda |
+----+--------+-------+
If I do a search for blue I would like to get red, green and blue as an answer. Right now I am using 2 queries. One to find the 'entity' number and one to find all the words with the same 'entity' number.
Try this. Join is much faster than subquery
select distinct t2.word from Table t1
INNER JOIN Table t2 on t2.entity=t1.entity
where t1.word="blue";
SELECT *
FROM TABLE_NAME
WHERE entity IN
(SELECT entity
FROM TABLE_NAME
WHERE word='blue');

Mysql Swap data between rows using a dummy table [duplicate]

This question already has answers here:
Switch id numbers of two rows in MySql
(5 answers)
Closed 9 years ago.
Suppose a table fruits that looks like this:
------------------------------------------
| id | name | color | calories |
------------------------------------------
| 1 | apple | red | 20 |
| 2 | orange | orange | 10 |
| 3 | grapes | green | 5 |
| 4 | bananas | yellow | 15 |
| 5 | plum | purple | 25 |
------------------------------------------
How can I swap the values of a row, with another, leaving the id number intact?
Example:
SWAP ROW WITH ID "5" WITH ROW WITH ID "2"
Result:
------------------------------------------
| id | name | color | calories |
------------------------------------------
| 1 | apple | red | 20 |
| 2 | plum | purple | 25 |
| 3 | grapes | green | 5 |
| 4 | bananas | yellow | 15 |
| 5 | orange | orange | 10 |
------------------------------------------
Note that all the values are intact except for the id. Note that this is a very large list in reality.
Note: id, and some of the values of the table are unique.
Note2: There's a question posted on this here, but it requires not using a dummy table, so I'd like to see the solution when the table has unique values other than just id.
Thank you
UPDATE fruit a, fruit b
SET a.color = b.color,
a.name = b.name,
a.calories = b.calories
WHERE a.id <> b.id
AND a.id in (2,5)
AND b.id in (2,5);
The following statements will work.
UPDATE fruits SET id=6 where id=2
UPDATE fruits SET id=2 where id=5
UPDATE fruits SET id=5 where id=6