How to select 3 row per category in MySQL? - mysql

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:

Related

SQL Find All user that have two history in a certain order

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.

Get the latest 3 nodes foreach taxonomy term in one query

I am trying to setup a query to get the 3 most recent nodes (nid) foreach taxonomy term (tid). Is this possible to set up on one query?
Here are how my tables are set up (not displaying full table info)
taxonomy_index (Holds the taxonomy ids that are associated to a node)
nid tid
1 20
1 21
1 22
2 20
2 21
3 23
3 24
4 20
4 21
5 20
5 21
5 22
5 23
6 20
6 21
6 24
7 20
7 21
8 20
8 21
9 20
9 21
9 22
9 23
.....
node (node information)
nid title created
1 Article One 1105350260
2 Article Two 1105350259
3 Article Three 1105350261
4 Article Four 1105350280
5 Article Five 1105350290
6 Article Six 1105350290
.....
I'm envisioning a result like this if I wanted to see the latest 3 nodes for tids: 20, 21, 22:
tid nid
20 1
20 2
20 4
21 1
21 2
21 4
22 1
22 5
22 9
Is this possible in one query?
You can try follwing query:-
SELECT t.tid, t.nid
FROM taxonomy_index s
WHERE (SELECT COUNT(*)
FROM taxonomy_index f
WHERE f.tid = s.tid
AND f.nid <= s.nid
) <= 3;
here i have used <=3 #cond, in case if any tid dont have 3 records the it will fetch 2 or 1, whichever is more.

SQL query syntax to retrieve data from 2 tables

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

SQL Count Average

I have table like
id userid semid courseid coursename total
1 36 17 13 CA 23
2 36 17 5 CB 46
3 36 17 8 CC 20
4 36 19 16 CD 34
5 36 19 13 CA 31
6 36 19 3 CA# 29
7 36 19 7 CE 60
8 36 10 9 CK 32
9 36 10 15 CH 56
I need average of semid for a userid i.e., SUM(courseid) /count (moduleid), It was showing 9 as module count, but I have only 3 modules.
This is my query
SELECT userid, SUM(total)/count(semid) FROM custom WHERE userid=36
just use the AVG( ) function
SELECT userid, semid, AVG(total)
FROM custom
WHERE userid = 36
GROUP BY userid, semid
SQLFiddle Demo
SELECT userid, SUM(total)/count(distinct semid) FROM custom WHERE userid=36
Try this query
There is MYSQL aggregate function AVG() for finding Average . #John Totet Woo has posted the answer.

count items in col as rows MYSQL

Dear developers and programmers,
I have a tabel with SHOP_ID, PROD_ID, ipadres and some more tables, but these are where its all about...
Looks like this:
id ip number timestamp shop_id prod_id
--------------------------------------------------
42 81.69.205.25 1319488326 2 3
43 81.205.141.48 1319492649 2 3
44 193.58.10.10 1319520579 14 17
45 84.28.22.226 1319529529 11 19
46 88.15.81.188 1319543745 2 1
47 178.17.241.191 1319563031 14 7
48 87.28.107.171 1319563038 2 6
49 80.156.47.144 1319572818 14 7
50 82.76.241.175 1319577506 11 1
51 82.76.241.175 1319577584 13 1
52 82.76.241.175 1319577785 14 1
53 82.76.241.175 1319577860 4 1
54 62.94.133.153 1319579221 14 1
55 62.94.133.153 1319579281 2 3
56 77.70.175.221 1319617238 11 1
57 77.70.175.221 1319621845 13 1
58 77.70.175.221 1319621848 2 1
59 77.70.175.221 1319621850 11 1
.... more
--------------------------------------------------------
Is there a way to see for each prod_id how many ip numbers there excist for each shop id?
output example
1 2 3 4 5
---------------------------------------------
1 18 5 51 8 4
2 58 5 45 3 4
3 7 6 31 9 2
where horizontal is the prod_id and vertical is the shop_id
I've don this already:
select
shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id
order by amount desc
but this wil give me only the result of all prod_id's combined.
I would like to have the prod_id's seperate in columns.
I hope there is a solution!
Kind Regards
Try this
select `prod_id`,shop_id,
count(distinct(ipadres)) amount
from table
GROUP BY shop_id,prod_id
order by amount desc