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
Related
This question already has answers here:
Select values that meet different conditions on different rows?
(6 answers)
Closed 4 years ago.
can I return a single instance of a row after using a join on a categories table.
Entries
| id | Name |
| 1 | Johnny |
| 2 | Steve |
| 3 | Bam |
Categories
| cat_id | Name |
| 1 | Season one |
| 2 | Season two |
| 3 | Season three|
Category Posts
| id | cat_id |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
What I want to do is select all cast where members that have been in season 2 and 3, they must have been in both and I only want a single instance returned.
Expected output
| id | Name |
| 1 | Johnny |
| 2 | Steve |
How would I got about selecting these? I've thought about grouping the user based on their name however because I'm selecting IN ("2", "3") I get some users that have been in two but not three and the expected results are wrong.
Thanks
you can use sub query
SELECT id,name from Entries where id in (select a.id from (select id from
category_post WHERE cat_id=2) as a,(select id from category_post WHERE cat_id=3) as b where a.id=b.id)
I want to show data that a user can modify. If the user do changes, i like keeping the original data in my database.
When a user get data, he should view the data modified.
(Much users can modiffied data).
For example:
Products (non editable for users)
ID | Name | Color
----------------------
1 | Apples | Yellow
2 | Pears | Green
3 | Lemons | Yellow
ModProducts (one row for modified product and user)
SourceID | UserID | Name | Color
-------------------------------------------
1 | 3 | RedApples | Red
1 | 4 | Sminth Apples | Green
Result for UserID 3:
ID | Name | Color
-------------------------
1 | RedApples | Red
2 | Pears | Green
3 | Lemons | Yellow
I tryed with COALESCE, CASE but i canĀ“t filter by UserID.
I Also tryed with GROUP BY, putting all data in same table, but I have not managed to give priority to the modified data
I'm going crazy..
Maybe you're designing the data model wrong?
You probably just misplaced the UserID filter. The filter on UserID has to be part of the outer join condition it can not be in the where clause. Try the following select:
select p.id,
coalesce(m.name, p.name) name,
coalesce(m.color, p.color) color
from products p
left join modproducts m on m.id = p.id and
m.userid = 3
Problem solved in one table
Table Products
ID | Ref | UserID | Name | Color
-------------------------------------------
1 | 1 | 0 | Apples | Yellow
2 | 2 | 0 | Strawberrys | Red
3 | 3 | 0 | Lemons | Yellow
4 | 1 | 2 | Apples | Green
5 | 1 | 3 | MiniApples | Green
I like this result
ID | Ref | UserID | Name | Color
-------------------------------------------
4 | 1 | 2 | Apples | Green
2 | 2 | 0 | Strawberrys | Red
3 | 3 | 0 | Lemons | Yellow
I resolv this form:
SELECT *
FROM ( SELECT * FROM Products ORDER BY Ref, UserID DESC) AS Prod
WHERE UserID = 2
OR UserID = 0
GROUP BY Ref
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 |
+----+------+---------+----------------+
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
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');