How to fetch all rows with group by value in mysql? - 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;

Related

how can i sort data by rows? [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;

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

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

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.