In mysql, I got table "a" as follow
ID USERNAME SUBJECT_ID
1 ALAN 1
2 ALAN 2
3 ALAN 3
4 ALAN 4
5 ALAN 5
6 JOHN 6
7 ALAN 7
8 JOHN 8
9 ALAN 9
I just want to know username = ALAN takes how many subject and how generate a new column to C become
ID USERNAME SUBJECT_ID C
1 ALAN 1 1
2 ALAN 2 2
3 ALAN 3 3
4 ALAN 4 4
5 ALAN 5 5
7 ALAN 7 6
9 ALAN 9 7
The column is group by USERNAME and generate the row count.How should I query and generate C field?
The query I have tried, but I don't know how to generate "C"
SELECT ID,USERNAME,SUBJECT_ID,C FROM a WHERE USERNAME = "ALAN" GROUP BY SUBJECT_ID
Please help, thanks.
You can use Row number in mysql, this way:
SELECT T1.*,#rownum := #rownum+1 as C
FROM TableName T1,(SELECT #rownum := 0) r
WHERE T1.Username LIKE '%ALAN%'
Result:
ID USERNAME SUBJECT_ID C
-----------------------------
1 ALAN 1 1
2 ALAN 2 2
3 ALAN 3 3
4 ALAN 4 4
5 ALAN 5 5
7 ALAN 7 6
9 ALAN 9 7
Example in SQL Fiddle.
Related
i have a table named test with the below structure like this
id mark join_id
1 5 1
2 4 1
3 9 1
4 5 2
5 7 2
6 12 2
i want to write a query that can get me the average of the marks from the beginning record to this record with the desired result as below
id mark join_id avg_of_previous_marks
1 5 1 5
2 4 1 4.5
3 9 1 6
4 5 2 5.75
5 7 2 6
6 12 2 7
i wrote this query but it doesn't seem to work correctly
SELECT test.id, test.mark, test.join_id, test_avg.avg_of_previous_marks FROM test
LEFT JOIN (SELECT id, join_id, AVG(mark) as avg_of_previous_marks FROM test GROUP BY
join_id) test_avg
ON test_avg.join_id = test.join_id AND test_avg.id <= test.id
and it gives this resault
id mark join_id avg_of_previous_marks
1 5 1 6
2 4 1 6
3 9 1 6
4 5 2 8
5 7 2 8
6 12 2 8
Its a simple running total that you need.
select id,mark,join_id, avg(mark) over (order by id) avg_of_previous_marks from test_avg ;
fiddle here
I've been having trouble linking these tables together:
Table 1: Matches
ID
Name
Date
1
Adam vs Lance
2021-09-2021
2
Bex vs Adam vs Erica
2021-08-2021
3
Craig vs Bree
2021-07-2021
4
Danielle vs Alan
2021-06-2021
5
Erica vs Zoe vs AJ
2021-05-2021
6
Bree vs Erica
2021-04-2021
7
Bree vs Lance
2021-03-2021
8
Bree vs Lance vs Zoe
2021-02-2021
Table 2: Winners:
ID
Name
Match ID
IDNum
1
Adam
1
1
2
Bex
2
3
3
Danielle
4
7
4
Zoe
5
9
5
Erica
6
4
6
Bree
7
5
7
Bree
8
5
Table 3: Losers:
ID
Name
Match ID
IDNum
1
Lance
1
2
2
Adam
2
1
3
Erica
2
4
4
Alan
4
8
5
AJ
5
10
6
Erica
5
4
7
Bree
6
5
8
Lance
7
2
9
Lance
8
2
10
Zoe
8
9
Table 3: Draws:
ID
Name
Match ID
IDNum
1
Craig
3
6
2
Bree
3
5
Table 4: Players
ID
Name
Gender
1
Adam
M
2
Lance
M
3
Bex
F
4
Erica
F
5
Bree
F
6
Craig
M
7
Danielle
F
8
Alan
M
9
Zoe
F
10
AJ
F
The query I've been trying is to look up all matches with Bree in them and order them by date.
Table 5: Output:
Match ID
3
6
7
8
Draw: Match ID: 3
Los: Match ID: 6
Win: Match ID: 7
Win: Match ID: 8
When I try to inner join wins & losses against the Match table it works but the second I include the draws it does not return anything.
If I try just returning draws it works but then inner joining either losses or wins causes 0 results.
Can anyone help me with the code that'll work?
Query I'm trying:
SELECT Matches.ID AS MatchID, Winners.Name
FROM Matches
inner JOIN Draws
ON Matches.ID = Draws.MatchID
inner JOIN Winners
ON Matches.ID = Winners.MatchID
inner JOIN Losers
ON Matches.ID = Losers.Match ID
and (Winners.winner_id_num = 5
OR
Losers.type_id_num = 5
OR
Draws.IDNum = 5
)
GROUP BY match_id_num;
I would recommend you to use UNION between the 3 tables with results and join the output with the Matches table.
SELECT r.Result, r.IDMatch FROM (
SELECT *, 'Win' as Result FROM Winners WHERE IDNum = 5
UNION
SELECT *, 'Los' as Result FROM Losers WHERE IDNum = 5
UNION
SELECT *, 'Draw' as Result FROM Draws WHERE IDNum = 5
) AS r
INNER JOIN Matches AS m ON m.ID = r.IDMatch
ORDER BY m.Date DESC
The output would be:
Draw 3
Los 6
Win 7
Win 8
Let me simplify this question.
Lets say that we have students and subjects.
Each Students can choose upto any 4 subjects.
Type A Design:
Students table
id Students_name
1 John
2 Jack
3 Jill
4 Nancy
Subjects table
id Subjects Students_id
1 Language 1
2 Maths 1
3 Science 1
4 History 1
5 Computer 2
6 Language 2
7 Maths 2
8 Science 2
9 History 3
10 Computer 3
11 Maths 3
12 Science 3
Type B Design
Same students table as type A
Subjects table
id Subjects
1 Language
2 Maths
3 Science
4 History
5 Computer
Another junction table is added here in this type as follows
students_subjects_mapping table
id Subjects_id Students_id
1 1 1
2 2 1
3 3 1
4 4 1
5 5 2
6 2 2
7 3 2
8 4 2
So which type is better for this scenario?
I have the table
id code name
=====================
1 30100 John
2 30100 Andrew
3 30100 Sandy
4 29145 Mike
5 29145 Tony
6 29145 Laura
7 29145 Henry
8 00124 Michael
9 00124 Teddy
10 13405 Andy
11 09325 Patrick
I want to select only 2 names grouped by code and get this result.
id code name
=====================
1 30100 John
2 30100 Andrew
4 29145 Mike
5 29145 Tony
8 00124 Michael
9 00124 Teddy
10 13405 Andy
11 09325 Patrick
Can somebody help me to make such query/queries?
Thanks
SELECT id, code, name
FROM TableName a
WHERE
(
SELECT count(*)
FROM TableName as f
WHERE f.code = a.code and a.id >= f.id
) <= 2
ORDER BY id, code, name
SQLFiddle Demo
I am trying make a complex query in MySQL i have following two tables
departments employees
Id parent title id name department status
--------------------------- ------------------------------------
1 0 Health 1 abc 3 1
2 0 Sports 2 def 3 1
3 0 Education 3 ghi 5 1
4 1 Physical 4 jkl 10 1
5 1 Mental 5 kkk 6 1
6 2 Football 6 lll 6 1
7 2 Baseball 7 sss 8 1
8 2 Beachball 8 xxx 6 1
9 2 Hockey 9 yyy 6 1
10 4 ENT 10 zzz 7 1
11 0 Finance 11 mnb 11 1
Departments table have four main departments(i-e: parent = 0) with multiple level depths of sub-departments.
Currently i have done it through a PHP function by running queries multiple time to achieve this, but still i want know if this is possible to fetch it with a Query.
Whats the best way OR how to select max 3 employee randomly for each main department with status 1.
The expected result should be something like
id name department maindep
1 abc 3 3
2 def 3 3
3 ghi 5 1
4 jkl 10 1
5 kkk 6 2
7 sss 8 2
10 zzz 7 2
11 mnb 11 11