sql many to many relationship with only 2 tables - mysql

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

Related

Selecting the rows with unique values after filtering duplicates

I'm trying to get the colors that are used by only a sole individual. Using the following sample data:
+---------+-------------+
| color | name |
+---------+-------------+
| red | Jane |
| red | Jane |
| red | Bob |
| blue | David |
| blue | Bill |
| green | Jack |
| green | Jack |
| purple | Jack |
| orange | Dan |
+---------+-------------+
I got rid of the duplicates with
SELECT color, name
FROM table
GROUP BY color, name;
+---------+-------------+
| color | name |
+---------+-------------+
| red | Jane |
| red | Bob |
| blue | David |
| blue | Bill |
| green | Jack |
| purple | Jack |
| orange | Dan |
+---------+-------------+
What do I need to do to filter it down further to my desired result set of green, purple, and orange only because only one person was associated with that color?
You can accomplish this by going one step further in your query. Take what you have as a subquery, then use it as the source for a second subquery that counts the number of distinct names that have shown up for each color. Then, it just becomes a select from a result set where the number of unique names was one.
SELECT color
FROM (SELECT color, COUNT(name) namecount FROM (SELECT color, name
FROM table GROUP BY color, name) t1
GROUP BY color) t2
WHERE namecount = 1

In MYSQL, how do I get the most common string in a column after filtering out duplicate users with SELECT INSTINCT?

Started learning MYSQL last week and I have a question. I'm trying to get the most popular color by amount of unique users from color_table (which would be Blue)
+---------+---------------+
| user | color |
+---------+---------------+
| John | Brown |
| Jack | Red |
| Stacy | Blue |
| Bob | Green |
| Jack | Red |
| Doe | Blue |
| Moe | Orange |
| Jack | Red |
| Jane | Blue |
| Nate | Purple |
| Jack | Red |
+---------+---------------+
The color Red has duplicates of the same user so I strip them out with the following:
SELECT INSTINCT user, color FROM color_table;
+---------+---------------+
| user | color |
+---------+---------------+
| John | Brown |
| Jack | Red |
| Stacy | Blue |
| Bob | Green |
| Doe | Blue |
| Moe | Orange |
| Jane | Blue |
| Nate | Purple |
+---------+---------------+
The table looks good afterwards but what do I do after this? Using SELECT INSTINCT on two columns, I can't get Count(*) and ORDERBY DESC and LIMIT 1 to work. I think I need to put them in a subquery but I'm not sure. I keep reading there are limitations to what SELECT INSTINCT can do so I'm not sure if that is the correct method to filter out the duplicates either for what I'm trying to do.
Key is to use distinct inside the count function.
SELECT color, count(distinct user) FROM color_table group by color;

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

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

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