I have 2 tables in Mysql: authors and articles.
I have authors with several articles for each of them.
I need sql query with the result of this query:
retrieve 3 authors ordered by age with all articles which belong each of them.
In my example it will be
ID FIRSTNAME LASTNAME AGE AUTHORID TITLE PRICE
6 Salido Gomes 90 6 All 3 1
6 Salido Gomes 90 6 All 3 33
6 Salido Gomes 90 6 All 3 3
5 Vitora Mantora 45 5 Total 3 99
5 Vitora Mantora 45 5 Total 3 33
5 Vitora Mantora 45 5 Total 3 12
3 Joe Smith 43 3 Python 5
3 Joe Smith 43 3 Python 2 22
3 Joe Smith 43 3 Python 3 44
3 Joe Smith 43 3 Python 4 12
3 Joe Smith 43 3 Python 5 67
http://sqlfiddle.com/#!2/718c4/1
I use
select * from authors join articles on authors.id = articles.authorId
join (select authors.id from authors order by age DESC limit 3) as t
on t.id = authors.id
with wrong result
ID FIRSTNAME LASTNAME AGE AUTHORID TITLE PRICE
6 Salido Gomes 90 6 All 3 1
6 Salido Gomes 90 6 All 3 33
6 Salido Gomes 90 6 All 3 3
If you only want to include authors who have written any articles in the top 3 oldest authors try doing a join between authors and articles in the sub query to get the 3 oldest authors, and DISTINCT to eliminate duplicates:-
SELECT *
FROM
(
SELECT DISTINCT authors.id, authors.firstname, authors.lastname, authors.age
FROM authors
JOIN articles
ON authors.id = articles.authorId
ORDER BY authors.age
DESC
LIMIT 3
) author_sub
JOIN articles
ON author_sub.id = articles.authorId
Related
Table1:
***Publisher*** with following columns:
o **Publisher_id** ; unique ID for each publisher
o **Video_id** ; unique ID for each video
o **Video_duration**; in minutes for each video
Table2:
***Consumer*** with the following columns:
o **Video_id** ; unique ID for each video
o **User_id** ; unique ID for each customer
o **Dateid** ; the date on which the consumer watched a video
Example:
Publisher_id Video_id Video_duration
1 1 2
1 2 3
1 3 2
1 4 5
2 6 2
2 7 2
2 8 20
3 10 2
3 11 3
3 12 3
1 23 100
5 9 8
Video_id User_id Date_id
1 44 20210615
2 44 20210615
11 33 20210614
6 44 20210612
I think I mostly need to figure out how to sum up the time spent watching videos per user_id and then to corroborate that using a limit 5 select, but I can't figure how to combine these 2 selects
I'd try like this:
SELECT SUM(Video_duration)
FROM Consumer
INNER JOIN Publisher USING (Video_id)
GROUP BY User_id
ORDER BY SUM(Video_duration) DESC
LIMIT 5
I'm looking for a SQL request that I can't find in internet (and I didn't found a solution myself).
I have two different table user and history and a table user_history that link the two tables.
For example :
USER
id name
1 John
2 Edie
3 France
4 Gabriel
5 Ellen
History
id date_entered type
1 2017-07-01 36
2 2017-07-02 52
3 2017-07-03 25
4 2017-07-04 69
5 2017-07-05 85
6 2017-07-06 74
7 2017-07-07 45
8 2017-07-08 85
9 2017-07-09 25
10 2017-07-10 78
USER_HISTORY
id id_user id_history
1 1 1
2 1 2
3 1 3
4 1 4
5 2 5
6 2 6
7 1 7
8 1 8
9 2 9
10 1 10
In this example, all history are made by user 1 and 2 (user 2 have history 5,6 and 9).
So the question is :
What is the SQL request that get me all the users that have in their history an history type 25 and then some days LATER an history type 85 ?
In this example, only user 1 (John) is ok because he has a history type 25 on 2017-07-03 and then an history type 85 on 2017-07-08.
User 2 (Edie) is not ok because even if he has an history 25 and 85, the first one was 85 and the 25.
Is that clear ?
Can you help me please ?
You need to JOIN twice with HISTORY table, e.g.:
SELECT h1.id_user
FROM (
SELECT u.id_user, h.date_entered
FROM user_history u
JOIN history h ON u.id_history = h.id
WHERE h.type = 25) h1
JOIN (
SELECT u.id_user, h.date_entered
FROM user_history u
JOIN history h ON u.id_history = h.id
WHERE h.type = 85
) h2 ON h1.id_user = h2.id_user
WHERE h1.date_entered < h2.date_entered;
Here's the SQL Fiddle.
I have a table which have a lot of data, it's have a category ID and postId, I need to read 3 new post per category with same CatID.
it's not duplicate of the question suggest by other people. Please check that in my question the postid catid can be anything when in duplicate question it's calculate before running query.
What I have written is
SELECT
MAX(` postid `) AS p1,
` catid ` AS c1
FROM
` postcategory `
GROUP BY
` catid
I can put 2 other query in it union distinct but it will make a query a lot big. Is there any good way to do this in MySQL. What I am looking for reading 3 postId (maximum) belong to same category.
postId catId
------ --------
9 3
15 3
16 3
17 3
18 3
19 5
20 8
21 6
22 8
23 6
46 6
46 8
26 3
25 3
27 5
28 3
37 6
39 10
40 6
41 6
42 6
43 6
44 5
45 11
63 6
64 5
65 6
66 6
68 6
You can read 3 new post from each category Using the below query.
SELECT
p1.postId,
p1.catId
FROM
postcategory p1
JOIN postcategory p2 ON p1.catId = p2.catId
AND p2.postId >= p1.postId
GROUP BY
p1.postId,
p1.catId
HAVING
COUNT(*) <= 3
ORDER BY
catId,
postId
Here you can see the Live Demo
Output:
Say I have a table called students
idStudent Name
1 Billy
2 Mariah
3 Chris
4 Mark
5 Sarah
and another table called tests
idTest score student_idstudent
1 50 1
2 100 1
3 90 2
4 100 3
5 45 4
is it possible to use a combination of a join and avg() to get a result like
idStudent avg_test
1 75
2 90
3 100
4 45
5 0
SELECT s.idStudent,
AVG(COALESCE(t.score, 0)) AS avg_test
FROM students s
LEFT JOIN tests t
ON s.idStudent = t.student_idStudent
GROUP BY s.idStudent
I am trying to select from three tables, Regions, Players, and regionplayer. and order the results by region name, and then by when the players attached to that region where last seen. I would really like it so that i only return one result for each region, with the player who hasn't been seen the longest.
SQL.
SELECT RegionName.*, RegionPlayer.*, Players.*
FROM RegionName
JOIN RegionPlayer
ON RegionPlayer.Regionkey = RegionName.Key
JOIN Players
ON Players.Key = RegionPlayer.Playerkey
GROUP BY RegionName.Name, Players.Seen DESC
some table data.
RegionName
key Name
1 regionone
2 regiontwo
3 regionthree
4 regionfouor
5 regionfive
Players
Key Name Seen
1 jack 2014-03-21 12:43:46
2 joe 2014-03-26 12:43:46
3 bob 2014-03-20 12:43:46
4 bill 2014-03-19 12:43:46
5 dave 2014-03-17 12:43:46
6 tina 2014-03-28 12:43:46
7 tony 2014-03-29 12:43:46
8 george 2014-03-15 12:43:46
9 sam 2014-03-18 12:43:46
10 frank 2014-03-18 12:43:46
RegionPlayer
key Regionkey PlayerKey
1 1 1
2 1 4
3 1 5
4 2 1
5 2 4
6 3 6
7 3 7
8 4 1
9 4 8
10 4 7
11 5 3
I take your statement, at first the part before GROUP BY
SELECT RegionName.*, RegionPlayer.*, Players.*
FROM RegionName
JOIN RegionPlayer
ON RegionPlayer.Regionkey = RegionName.Key
JOIN Players
ON Players.Key = RegionPlayer.Playerkey
and add
WHERE Players.Seen = (
SELECT MIN(Players2.Seen)
FROM RegionName as RegionName2
JOIN RegionPlayer as RegionPlayer2
ON RegionPlayer2.Regionkey = RegionName2.Key
JOIN Players as Players2
ON Players2.Key = RegionPlayer2.Playerkey
WHERE RegionName2.Key = RegionName.Key
)
and then GROUP BY from your statement
GROUP BY RegionName.Name, Players.Seen DESC
The subselect returns the minimum date SEEN from PLAYERS for current region.