How to sort the result of multiple queries alternatively? - mysql

I have a query made of three select clause like this:
select id, colors from table1
union all
select id, numbers from table2
union all
select id, names from table3
Also here is the tables structure:
// table1 // table2 //table3
+----+--------+ +----+---------+ +----+-------+
| id | colors | | id | numbers | | id | names |
+----+--------+ +----+---------+ +----+-------+
| 1 | red | | 1 | ten | | 1 | jack |
| 2 | green | | 2 | two | | 2 | peter |
| 3 | blue | | 3 | one | +----+-------+
| 4 | yellow | | 4 | three |
+----+--------+ | 5 | six |
| 6 | five |
+----+---------+
Now I want this order for the results:
+----+--------+
| id | colors |
+----+--------+
| 1 | red |
| 2 | ten |
| 3 | jack |
| 4 | green |
| 5 | two |
| 6 | peter |
| 7 | blue |
| 8 | one |
| 9 | yellow |
| 10 | three |
| 11 | six |
| 12 | five |
+----+--------+
How can I implement that? (it should be noted, order by 1,2,3 does not work for me)

This is how you can do this
select #rn:=#rn+1 as id,colors from (
(select #rn1:= #rn1+1 as rn,colors from table1,(select #rn1:=0)x order by id )
union all
(select #rn2:= #rn2+1 as rn,numbers as colors from table2,(select #rn2:=0.5)x order by id)
union all
(select #rn3:= #rn3+1 as rn,names as colors from table3,(select #rn3:=0.6)x order by id )
)x,(select #rn:=0)y order by rn ;
The idea is to assign a rn value for each table item and need to make sure that these values are always in ascending order
So if you run the query for each table you will have
mysql> select #rn1:= #rn1+1 as rn,colors from table1,(select #rn1:=0)x order by id;
+------+--------+
| rn | colors |
+------+--------+
| 1 | red |
| 2 | green |
| 3 | blue |
| 4 | yellow |
+------+--------+
4 rows in set (0.00 sec)
mysql> select #rn2:= #rn2+1 as rn,numbers as colors from table2,(select #rn2:=0.5)x order by id;
+------+--------+
| rn | colors |
+------+--------+
| 1.5 | ten |
| 2.5 | two |
| 3.5 | one |
| 4.5 | three |
| 5.5 | six |
| 6.5 | five |
+------+--------+
6 rows in set (0.00 sec)
mysql> select #rn3:= #rn3+1 as rn,names as colors from table3,(select #rn3:=0.6)x order by id;
+------+--------+
| rn | colors |
+------+--------+
| 1.6 | jack |
| 2.6 | peter |
+------+--------+
2 rows in set (0.00 sec)
Here you can see table1 rn values are 1,2,3,....
table2 values are 1.5,2.5,3.5,....
table3 values are 1.6,2.6,....
so finally when you order the result with all rn it will be as
1,1.5,1.6,2,2.5,2.6,....

Related

MYSQL group by and counting columns

Say I have a table like this:
+--------+-------+
| ID | LETTER|
+--------+-------+
| 1 | A |
| 1 | A |
| 1 | A |
| 2 | A |
| 2 | B |
| 3 | C |
| 3 | D |
| 4 | D |
+--------+-------+
How would I implement a query that returns the ID followed by how many unique letters it has? So it would look like this:
+--------+-------+
| ID | LETTER|
+--------+-------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
+--------+-------+
I've tried using group by but I cant figure it out.
You should search for the answer before questioning. But here is what you need:
SELECT yt.ID, COUNT(DISTINCT yt.LETTER)
FROM yourtable yt
GROUP BY yt.ID

MYSQL: How to select every 4th row STARTING from 3rd row?

meaning the result should be selecting 3rd, 7th, 11th, 15 rows etc.
Every row has an ID, in ascending order.
I am stuck on this for hours! Any help is highly appreciated!
A simpler way would be to use mod on the id itself:
select * from table where (id + 1) mod 4 = 0;
Suppose your schema is:
CREATE TABLE t1 (
id int(11) DEFAULT NULL,
data varchar(20) DEFAULT NULL
);
mysql> select * from t1;
+------+--------+
| id | data |
+------+--------+
| 1 | abc-1 |
| 2 | abc-2 |
| 3 | abc-3 |
| 4 | abc-4 |
| 5 | abc-5 |
| 6 | abc-6 |
| 7 | abc-7 |
| 8 | abc-8 |
| 9 | abc-9 |
| 10 | abc-10 |
| 11 | abc-11 |
| 12 | abc-12 |
| 13 | abc-13 |
| 14 | abc-14 |
| 15 | abc-15 |
| 16 | abc-16 |
+------+--------+
16 rows in set (0.00 sec)
mysql> select id,data from (select mod(#r:=#r+1,4) as isfetch,id,data from t1,(select #r:=0) s) k where k.isfetch=0 order by id;
+------+--------+
| id | data |
+------+--------+
| 4 | abc-4 |
| 8 | abc-8 |
| 12 | abc-12 |
| 16 | abc-16 |
+------+--------+
4 rows in set (0.01 sec)

MySQL group by names separated by commas

I received a CSV file and one of the columns has names like Jack,Clark,James in one cell. Another will just have James, then another with be Clark,James. Is there a way I can GROUP BY and COUNT(*) without giving them all their own rows?
Ignoring for a moment the old adage that says "just because you can do something, it doesn't mean that you should", and noting the presence of a PRIMARY KEY in my data set, which is absent from yours...
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT * FROM my_table;
+----+--------------------+
| id | names |
+----+--------------------+
| 1 | steve,james,carlos |
| 2 | james |
| 3 | carlos,steve |
| 4 | james,jack |
+----+--------------------+
SELECT name
, COUNT(*) total
FROM
( SELECT DISTINCT id
, SUBSTRING_INDEX(SUBSTRING_INDEX(names,',',i),',',-1) name
FROM my_table x
, ints
) a
GROUP
BY name;
+--------+-------+
| name | total |
+--------+-------+
| | 4 |
| carlos | 2 |
| jack | 1 |
| james | 3 |
| steve | 2 |
+--------+-------+

update query is not working - mysql

Following is my table structure:
+------+----------+
| product | seq |
+------+----------+
| a | 1 |
| b | 1 |
| a | 2 |
| a | 3 |
| b | 2 |
+------+----------+
If my inputs are a and 2 then, i need to change the sequence number and the the output will be
+------+----------+
| product | seq |
+------+----------+
| a | 2 |
| b | 1 |
| a | 1 |
| a | 3 |
| b | 2 |
+------+----------+
am using the logic as :
update mytable SET seq=if(seq=2,1,2), seq=if(seq=1,2,1) where product='a'
But it will not make any changes in the table
Try Like this
UPDATE table1 s1 , table1 s2 SET s1.seq=s2.seq,
s2.seq=s1.seq WHERE s1.seq=2 and s2.seq=1 and
s1.product='a' and s2.product='a';

Mysql : select count when id have multiple same value

+------+---------+
| id | object |
+------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
| 3 | 3 |
| 3 | 4 |
+------+---------+
i want to select count id where have a same value, so the result be, id 1 have 4 same value, id 2 have 2 same value, id 3 have 3 same value .
+------+
| id |
+------+
| 4 |
| 2 |
| 3 |
+------+
thanks for help, master.
SELECT id, COUNT(object) FROM tablename GROUP BY id
SELECT COUNT( * ) FROM `test` GROUP BY id