how can i sort data by rows? [MYSQL] - mysql

my database
id
title
create_by
1
aaa
2
2
bbb
1
3
ccc
3
4
ddd
2
5
eee
2
6
fff
3
what i want. sort by row that have same most value.
id
title
create_by
1
aaa
2
4
ddd
2
5
eee
2
3
ccc
3
6
fff
3
2
bbb
1
Thank you very much.

WITH cte AS ( SELECT id,
title,
create_by,
COUNT(create_by) OVER (PARTITION BY create_by) cnt
FROM src_table )
SELECT id,
title,
create_by
FROM cte
ORDER BY cnt, id;

Related

How to get row occurrence value in MySQL?

I have a following table
id name amount
1 aaa 1000
2 bbb 1500
3 ccc 1700
4 ddd 2000
5 aaa 1400
6 aaa 1700
7 bbb 1800
What I need is one more column to display the occurrence value based on the name as follows
id name amount occurrence
1 aaa 1000 1
2 bbb 1500 1
3 ccc 1700 1
4 ddd 2000 1
5 aaa 1400 2
6 aaa 1700 3
7 bbb 1800 2
You can do it with a correlated subquery:
SELECT id, name, amount,
(SELECT COUNT(*)
FROM mytable AS t2
WHERE t2.name = t1.name AND t2.id <= t1.id) AS occurrence
FROM mytable AS t1
Demo here

How to fetch all rows with group by value in mysql?

I have a table say table1 which has 3 columns like id, name and age.
Records available in table1,
id name age
-------------------
1 xxx 12
2 yyy 21
3 zzz 12
4 aaa 19
5 ccc 21
6 fff 12
I need result like this,
id name age same_age_count
-----------------------------------
1 xxx 12 3
2 yyy 21 2
3 zzz 12 3
4 aaa 19 1
5 ccc 21 2
6 fff 12 3
This is what my expected result.
Note:
same_age_count is the count value of repeated age in the table.
I have tried this query,
select *, count(age) as same_age_count from table1 group by(age);
id name age same_age_count
-----------------------------------
1 xxx 12 3
2 yyy 21 2
3 aaa 19 1
but it returns 3 records only, please give me a query for my expected result..
Thank you.
Try this:
SELECT T1.*,T2.same_age_count
FROM TableName T1
JOIN
(SELECT age,Count(age) as same_age_count
FROM TableName
GROUP BY age) T2
ON T1.age=T2.age
Result:
ID NAME AGE SAME_AGE_COUNT
1 xxx 12 3
2 yyy 21 2
3 zzz 12 3
4 aaa 19 1
5 ccc 21 2
6 fff 12 3
See result in SQL Fiddle.
If you are using mysql 8 or above you can use window functions.
select t1.*, count(t1.age) over (partition by t1.age) as same_age_count from table1 t1;

MySQL: Update all rows in 2 table matching results of another query

I have two table with same id, i'd like to change the id for both table with the value from 2nd table.
Ex:
Table_1
ID TITLE CONTENT AUTHOR
1 aaa abcd aaaa
2 aaa abcd aaaa
3 aaa abcd aaaa
4 aaa abcd aaaa
Table_2:
ID post_id post_title pos_key value
1 1 aaa a_key 2
2 1 aaa E_key 2000
3 2 aaa b_key 3
4 2 aaa b_key 3
5 2 aaa E_key 2500
6 3 aaa c_key 4
7 3 aaa d_key 5
8 3 aaa E_key 3000
9 4 aaa f_key 6
10 4 aaa E_key 3500
How can i update all Table_1 ID and Table_2 post_id from the value of Table_2 which have E_key on post_key row ?.
This is what i want for my both table:
Table_1
ID TITLE CONTENT AUTHOR
2000 aaa abcd aaaa
2500 aaa abcd aaaa
3000 aaa abcd aaaa
3500 aaa abcd aaaa
Table_2:
ID post_id post_title pos_key value
1 2000 aaa a_key 2
2 2000 aaa E_key 2000
3 2500 aaa b_key 3
4 2500 aaa b_key 3
5 2500 aaa E_key 2500
6 3000 aaa c_key 4
7 3000 aaa d_key 5
8 3000 aaa E_key 3000
9 3500 aaa f_key 6
10 3500 aaa E_key 3500
What have you tried? A good start is to write a SELECT query which pulls both the old and new values; from there it's usually simple to rewrite it as an UPDATE. I believe this will do it:
UPDATE T1
SET ID = T2.Value
FROM Table_1 AS T1
INNER JOIN Table_2 AS T2 ON T1.ID = T2.post_id
WHERE
T2.pos_key = 'E_key'
UPDATE T2
SET post_id = NewIDs.Value
FROM Table_2 AS T2
INNER JOIN Table_2 AS NewIDs ON T2.post_id = NewIDs.post_id
WHERE
NewIDs.pos_key = 'E_key'
My apologies if my MySQL is off, I usually work in MS SQL.

mysql order by asc issue

I have a application where we need to get the record order by asc but the value with zero record will be in last.
Have anyone help me regarding.
Here is my table structure
id name priority
1 abc 3
2 xyz 6
3 aaa 0
4 bbb 1
5 ccc 1
6 ddd 0
7 fff 2
want something like this
id name priority
1 bbb 1
2 ccc 1
3 fff 2
4 abc 3
5 xyz 6
6 aaa 0
7 ddd 0
SELECT *
FROM mytable
ORDER BY priority = 0, priority
SQLFiddle demo.
Try the following SQL:
SELECT * FROM (SELECT * FROM Table1 WHERE PRIORITY > 0 ORDER BY PRIORITY) a
UNION
SELECT * FROM Table1 WHERE PRIORITY = 0
SQL FIDDLE DEMO

Getting only 1 record from each category for each page in pagination

My table structure is as follows
id int
name varchar 50
catid int
Sample data
id name catid
---------------------------------------------------------
1 AAA 1
2 BBB 1
3 CCC 1
4 DDD 2
5 EEE 2
6 FFF 1
7 GGG 2
8 HHH 2
9 III 1
I want query such as it get me 1 row from each category for each page in pagination
Now for 1st Page I need data as
id name catid
---------------------------------------------------------
1 AAA 1
4 DDD 2
Now for 2nd Page I need data as
id name catid
---------------------------------------------------------
2 BBB 1
5 EEE 2
Now for 3rd Page I need data as
id name catid
---------------------------------------------------------
3 CCC 1
7 GGG 2
and so on.
How can I achieve this.
SELECT * FROM table WHERE catid = 1 LIMIT :pageno 1
SELECT * FROM table WHERE catid = 2 LIMIT :pageno 1
I think, this will be simplest query to get required result.
This worked for me (MySQL 5.1 on Linux)
SELECT id, name, catid FROM
(SELECT id, name, catid FROM t WHERE catid=1 LIMIT 2,1) AS t1
UNION (SELECT id, name, catid FROM t WHERE catid=2 LIMIT 2,1)
ORDER BY catid
The 2 in the limit is the page number. Hence the output of this one is for the second page.
Yes, as Pradeep said, a LIMIT clause would be the simplest way to do it in MySQL. You'll still need a little bit of coding in PHP to produce clickable page numbers for your users.