I am struggle with mysql query. please help me.
This is my query, i getting correct result but i need to modify the result in mysql.
SELECT bu.username,
bg.id as goal_id,
br.id as reason_id,
(SELECT COUNT(test_reason_id) FROM test_rank WHERE test_reason_id = br.id) as point
FROM
test_goal AS bg INNER JOIN test_reason AS br ON
br.user_id=bg.user_id INNER JOIN test_user AS bu ON
br.user_id=bu.id
WHERE
bg.id = br.test_goal_id
GROUP BY
bg.id
ORDER BY
point DESC
Tabble-1
My actual table look like this when i use ORDER BY point DESC then its look like Table-2
username goal_id reason_id point
khan 8 3 2
john 6 9 5
yoyo 5 21 4
smith 11 6 5
Tabble-2
My result set look like this
username goal_id reason_id point
john 6 9 5
smith 11 6 5
yoyo 5 21 4
khan 8 3 2
But i want my result set like this
username goal_id reason_id point rank
john 6 9 5 1
smith 11 6 5 2
yoyo 5 21 4 3
khan 8 3 2 4
is this possible? please can any one help me. it too difficult for me.
Add a row count variable like this:
select a.*, (#row := #row + 1) as rank
from (
SELECT bu.username,
bg.id as goal_id,
br.id as reason_id,
(SELECT COUNT(test_reason_id) FROM test_rank WHERE test_reason_id = br.id) as point
FROM
test_goal AS bg INNER JOIN test_reason AS br ON
br.user_id=bg.user_id INNER JOIN test_user AS bu ON
br.user_id=bu.id
WHERE
bg.id = br.test_goal_id
GROUP BY
bg.id
ORDER BY
point DESC
) a, (SELECT #row := 0) r
See this simplified SQLFiddle example
Related
Would like to seek your help on below:
I want to update the lessonnumber in Table lessons, the logic is lessonnumber will be update listed by order based on the starttime. My laravel code below works in MySQL, but error order occurred in MariaDB.
php
DB::statement('set #i=0;');
$type_id = 2; //lessons type
DB::update("
UPDATE lessons A
INNER JOIN (SELECT * FROM lessons WHERE lessons.type_id = $type_id ORDER BY lessons.starttime ASC LIMIT 30) B
ON B.id = A.id
SET A.lessonnumber = (select #i:=#i+1)
");
id
lessonnumber
startime
1
1
11
2
3
33
3
2
22
4
4
44
5
5
55
Mysql: lessonnumber 1 3 2 4 5 is correct order
id
lessonnumber
startime
1
1
11
2
2
33
3
3
22
4
4
44
5
5
55
MariaDB: lessonnumber 1 2 3 4 5 is error order
Can anyone help to solve my problem? Thank you in advance for your help
You are joining on id so the ordering in the join query is ignored. The correct form of update with order by is https://dev.mysql.com/doc/refman/8.0/en/update.html
SET #I = 0;
UPDATE LESSONS A
SET A.lessonnumber = (select #i:=#i+1)
WHERE TYPE_ID = 2
ORDER BY STARTTIME;
I am struggling at writing a query to get data from a table like this:
id food_id ingre_id
1 1 13
2 1 9
3 2 13
4 3 5
5 4 9
6 4 10
7 5 5
Assume in that table, each food only have 1 or 2 ingre ids. Then I want to have a table like this:
item_id ingre1_id ingre2_id
1 13 9
2 13 null //any food that have 1 ingre then set ingre2_id to null
3 5 null
4 9 10
5 5 null
Please suggest me a query to do such conversion. Thank you!
You can use aggregation. If you don't care about the ordering within a row:
select food_id, min(ingred_id) as ingred1_id,
(case when min(ingred_id) <> max(ingred_id) then max(ingred_id) end) as ingred2_id
from t
group by food_id;
Note: This use of min()/max() works specifically because you have two values. If you have more values, then ask another question with appropriate data.
This should produce what you asked for:
SELECT
a.`food_id` as `item_id`,
a.`ingre_id` as `ingre1_id`,
b.`ingre_id` as `ingre2_id`
FROM `food` a
LEFT JOIN `food` b
ON a.`id` <> b.`id` AND a.`food_id` = b.`food_id`
WHERE a.`id` < b.`id` OR b.`id` IS NULL
GROUP BY a.`food_id`
I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;
I have these three tables below. I want to join them using a MySQL query. Below the tables I've provided also how my output should look like.
GameTable:
GameID GameName
1 NvsA
2 NvsB
3 DvsA
4 NvsE
PlayerOnGame:
GameID PlayerName PlayerNumber PlayerID
1 Clement 7 10
1 Niyoyita 8 11
2 Clement 8 10
2 David 6 5
PlayerdataTable:
GameID Action PlayerNumber
1 kick 7
1 pass 8
1 Run 7
1 Kick 7
2 Kick 8
2 Pass 6
2 Run 8
2 Run 8
Output for only PlayerName = Clement:
GameName Action ActionCount
NvsA Kick 2
NvsA Run 1
NvsB Kick 1
NvsB Run 2
Please can someone help me with the query to get this output?
You can use this query to count
SELECT
gt.GameName,
pdt.Action,
count(pdt.Action) AS ActionCount
FROM GameTable AS gt
INNER JOIN PlayerdataTable AS pdt ON pdt.GameID = gt.GameID
INNER JOIN PlayerOnGame AS pg ON pg.GameID = pdt.GameID AND pg.PlayerNumber = pdt.PlayerNumber
WHERE pg.PlayerName = 'Clement'
GROUP BY gt.GameID , pdt.Action
I've tried a few of the similar SO questions, but I can't seem to figure it out.
On the first inner join, I only want to bring in DISTINCT function columns code and serial_id. So when I do my SUM selects, it calculates one per distinct. Ie there are multiple rows with the same func.code and func.serial_id. I only want 1 of them.
SELECT
sl.imp_id,
lat.version,
SUM(IF(lat.status = 'P',1,0)) AS powered,
SUM(IF(lat.status = 'F',1,0)) AS functional
FROM slots sl
INNER JOIN functions func ON sl.id = func.slot_id
INNER JOIN latest_status lat ON lat.code = func.code
AND lat.serial_id = func.serial_id
WHERE sl.id=55
GROUP BY sl.imp_id, lat.version
EDIT 2 - sample data explanation -------------------
slots - id, imp_id, name
functions - id, slot_id, code, serial_id
latest_status - id, code, serial_id, version, status
**slots**
id imp_id name
1 5 'the name'
2 5 'another name'
3 5 'name!'
4 5 'name!!'
5 5 'name!!!'
6 5 'testing'
7 5 'hi'
8 5 'test'
**functions**
id slot_id code serial_id
1 1 11HRK 10
2 2 22RMJ 11
3 3 26OLL 01
4 4 22RMJ 00
6 6 11HRK 10
7 7 11HRK 10
8 8 22RMJ 00
**latest_status**
id code serial_id version status
1 11HRK 10 1 F
1 11HRK 10 2 P
3 22RMJ 11 1 P
4 22RMJ 11 2 F
5 26OLL 01 1 F
6 26OLL 01 2 P
7 22RMJ 00 1 F
8 22RMJ 00 2 F
After running the query, the result should look like this:
imp_id version powered functional
5 1 1 3
5 2 2 2
The function table gets rolled up based on the code, serial_id. 1 row per code, serial_id.
It then gets joined onto the latest_status table based on the serial_id and code, which is a one (functions) to many (latest_status) relationship, so two rows come out of this, one for each version.
How about using DISTINCT?
SELECT
SUM(IF(lat.status = 'P',1,0)) AS powered,
SUM(IF(lat.status = 'F',1,0)) AS functional
FROM slots sl
INNER JOIN (Select DISTINCT id1, code, serial_id from functions) f On sl.rid = f.id1
INNER JOIN latest_status lat ON lat.code = f.code
AND lat.serial_id = f.serial_id
WHERE sl.id=55
GROUP BY sl.imp_id, lat.version
If you want only the distinct code and serial_id, you need to group by those not the imp_id and version. And end up with something like
SELECT
SUM(IF(lat.status = 'P',1,0)) AS powered,
SUM(IF(lat.status = 'F',1,0)) AS functional
FROM slots sl
INNER JOIN functions func ON sl.rid = func.id1
INNER JOIN latest_status lat ON lat.code = func.code
AND lat.serial_id = func.serial_id
WHERE sl.id=55
GROUP BY func.code, func.serial_id
However, this could all be rubish, without more data as tgo what some of those other columns are, but they dont seem to be the ones you wanted to group by.