V
V
v7resspect2017-06-21 17:38:14
MySQL
v7resspect, 2017-06-21 17:38:14

How to select records and two more records to them in mysql?

There are 2 tables: sources and articles .
You need to select 4 entries from sources and 2 entries from articles for them .
I make 1 request at once: And the second one in each iteration of the loop:
SELECT * FROM sources ORDER BY id DESC LIMIT 4

SELECT * FROM articles WHERE source_id='$id' ORDER BY id DESC LIMIT 2

Is it possible to somehow organize all this in one request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem0071, 2017-06-21
@Artem0071

Subquery?

SELECT * FROM articles WHERE source_id IN (SELECT * FROM sources ORDER BY id DESC LIMIT 4) ORDER BY id DESC LIMIT 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question