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 |
+----+------+---------+----------------+
Related
I need insert data from static table to table with multiple custom options, so it looks like below.
A table:
+------+------+--------+-------+
| name | type | weight | color |
+------+------+--------+-------+
| 1 | A | 10 | green |
+------+------+--------+-------+
| 2 | B | 3 | blue |
+------+------+--------+-------+
| 3 | D | 9 | gold |
+------+------+--------+-------+
Desired Output:
+------+-------------+--------------+
| name | option_name | option_value |
+------+-------------+--------------+
| 1 | type | A |
+------+-------------+--------------+
| 1 | weight | 10 |
+------+-------------+--------------+
| 1 | color | green |
+------+-------------+--------------+
| 2 | type | B |
+------+-------------+--------------+
| 2 | weight | 3 |
+------+-------------+--------------+
| 2 | color | blue |
+------+-------------+--------------+
| 3 | type | D |
+------+-------------+--------------+
| 3 | weight | 9 |
+------+-------------+--------------+
| 3 | color | gold |
+------+-------------+--------------+
Is it possible?
You can use cross join trick to "UNPIVOT" the values:
select
t.name,
case x.i
when 1 then 'type'
when 2 then 'weight'
when 3 then 'color'
end option_name,
case x.i
when 1 then type
when 2 then cast(weight as char(50))
when 3 then color
end option_value
from your_table t
cross join (
select 1 i union all
select 2 i union all
select 3 i
) x
cast(weight as char(50)) is required for weight because the data types need to be consistent and weight is (probably) a numeric column and needs to be converted into string.
SQLFiddle
I am running a query like this -
UPDATE table
SET eligible=1
where (name, age) in [["john",29],["mark",28],["jim",20]]
table structure is like this
name age eligible
mark 28 null
john 29 null
Max 20 null
But mysql throws the wrong syntax error, can anyone confirm that this is allowed in mysql?
SELECT * FROM my_table ORDER BY id LIMIT 10;
+----+-------+--------+
| id | type | count |
+----+-------+--------+
| 1 | green | 4 |
| 2 | blue | 3 |
| 3 | blue | 443792 |
| 4 | green | 455353 |
| 5 | blue | 445389 |
| 6 | blue | 360885 |
| 7 | green | 468258 |
| 8 | red | 258636 |
| 9 | blue | 388405 |
| 10 | green | 166117 |
+----+-------+--------+
SELECT *
FROM my_table
WHERE (type,count) IN (('red',1000),('green',2000),('blue',3000)) ORDER BY id LIMIT 100
-> ;
+--------+-------+-------+
| id | type | count |
+--------+-------+-------+
| 137339 | blue | 3000 |
| 339554 | red | 1000 |
| 947445 | green | 2000 |
+--------+-------+-------+
Note that MySQL can have problems utilising indexes when using this method. For that reason, it can prove more efficient to write the query out 'long-hand'.
If you're on an older version of MySQL, or an alternative way is to separate out the where statements:
UPDATE table
SET eligible=1
WHERE
(name = "john" AND age = 29) OR
(name = "mark" AND age = 28) OR
(name = "jim" AND age = 20)
I have a table like this:
// numbers
+----+--------+
| id | numb |
+----+--------+
| 1 | zero |
| 2 | one |
| 3 | two |
| 4 | three |
| 5 | four |
| 6 | five |
| 7 | six |
| 8 | seven |
| 9 | eight |
| 0 | nine |
+----+--------+
Now I'm trying to copy/paste the value of each row (just numb column) to the upper column. So this is expected result:
+----+--------+
| id | numb |
+----+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
| 5 | five |
| 6 | six |
| 7 | seven |
| 8 | eight |
| 9 | nine |
| 0 | zero |
+----+--------+
Actually I can do that by PHP. I mean I can fetch all rows and shift one itam and then update them. But I want to know can I do that by pure mysql?
All the rows except the max of id will get updated. The max id will still have the same numb. (in this case 9,'eight')
update tablename t1
JOIN tablename t2 on t1.id = t2.id-1
set t1.numb = t2.numb;
Sample Fiddle
Maybe something like
How do I UPDATE from a SELECT in SQL Server?
and use the id+1 for table2.
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');
I'd like to find the car_id's of the cars that have 'FORD' AND 'SILVER' AND the user input value of '200' in the value column:
table_cars
+----+--------+----------+-----------+
| id | car_id | name | value |
+----+--------+----------+-----------+
| 1 | 1 | MAKE | FORD |
| 2 | 1 | CARLINE | FIESTA |
| 3 | 1 | COLOR | SILVER |
| 4 | 1 | TOPSPEED | 210KM/H |
| 5 | 2 | MAKE | FORD |
| 6 | 2 | CARLINE | FOCUS |
| 7 | 2 | COLOR | SILVER |
| 8 | 2 | TOPSPEED | 200KM/H |
| 9 | 3 | MAKE | HOLDEN |
| 10 | 3 | CARLINE | ASTRA |
| 11 | 3 | COLOR | WHITE |
| 12 | 3 | TOPSPEED | 212KM/H |
+----+--------+----------+-----------+
Which in this case should return only one car_id: car_id = 2.
What would be the way to go to create the SQL query for this?
What you have is a properties table. When you want to test multiple properties at once you need to join the table to itself:
SELECT c0.car_id
FROM table_cars AS c0
JOIN table_cars AS c1 ON c1.car_id=c0.car_id
JOIN table_cars AS c2 ON c2.car_id=c1.car_id
WHERE c0.name='MAKE' AND c0.value='FORD'
AND c1.name='COLOR' AND c1.value='SILVER'
AND c2.name='TOPSPEED' AND c2.value='200KM/H'
Having the surrogate id present in a properties table is questionable. It doesn't seem to be doing anything; each property isn't an entity of its own. Unless the id is required by some other element, I'd get rid of it and make car_id, name the primary key (a composite primary key).
I assume that every car needs to have variable parameters, otherwise you wouldn't have gone with a setup like this. It would be much easier if MAKE, CARLINE, COLOR, and TOPSPEED each had their own column.
Using the table you've provided, however, you need to use subqueries. http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
The query should look something like this (untested):
SELECT * FROM table_cars WHERE id IN (SELECT * FROM table_cars WHERE name="MAKE" AND value="FORD") AND id IN (SELECT * FROM table_cars WHERE name="COLOR" AND value="SILVER") AND id IN (SELECT * FROM table_cars WHERE name="TOPSPEED" AND value="200KM/H")