M
M
mapleart2018-07-04 21:42:35
SQL
mapleart, 2018-07-04 21:42:35

How to write SQL select?

Hello! Faced such a problem.
There are two tables: topic and topic_tag
5b3d13c2724ce914198491.png
and
5b3d13cf6681a772973258.png
They are related by topic_id
field I need to select all elements from topic if it has one of given tags (For example, IN('website', 'demo', 'test'))
I tried to do So:

SELECT DISTINCT
  t.topic_id
FROM
  topic as t
JOIN topic_tag as tag on tag.topic_id = t.topic_id AND tag.topic_tag_text IN  ('сотрудники', 'Директор')
WHERE
  1=1
ORDER BY t.topic_id desc
LIMIT 0, 10

And when I want to make a selection on the second page LIMIT 1, 10, then the selection is no different. Can you suggest what can be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2018-07-04
@Rsa97

LIMIT <первая_запись>, <количество_записей>
Given that the records are numbered from 1, the selections LIMIT 0, 10and are LIMIT 1, 10equivalent.

J
johovich, 2018-07-04
@johovich

You overdid it with the request. If you only need topic_id from 1 table, you don't need to join at all to do
SELECT t.topic_id FROM topic as t
WHERE
t.topic_id in (SELECT topic_id FROM topic_tag WHERE topic_tag_text IN ('employees', 'Director')) ORDER BY t. topic_id desc
Run a request in phpmyadmin and see how the limit is set there when switching pagination, it will be clear how you need to do it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question