How to get row occurrence value in MySQL? - 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

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 MYSQL compare values from same columns

how do i compare 1 table in mysql?
with the following table
name table belajar
id model name
1 AAA AAA
2 BBB BBB
3 CCC CCC
and I want to generate a query
id name model idmodel namemodel modelmodel
1 AAA AAA 2 BBB BBB
1 AAA AAA 3 CCC CCC
2 BBB BBB 1 AAA AAA
2 BBB BBB 3 CCC CCC
3 CCC CCC 1 AAA AAA
3 CCC CCC 2 BBB BBB
It sounds like maybe a self join is what you want here:
SELECT
t1.id,
t1.name,
t1.model,
t2.id AS idmodel,
t2.name AS namemodel,
t2.model AS modelmodel
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.name <> t2.name AND
t1.model <> t2.model;
ORDER BY
t1.id,
t2.id;
The join criteria is basically a cross join, except that you don't want to match two records if the names and models be the same.

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