O
O
oliyanamega2019-12-06 10:36:47
MySQL
oliyanamega, 2019-12-06 10:36:47

How to generate a sql query to select from two tables?

There are two tables:
1. id|title|description
2. id|id_iz_pervoi_tablici|flag
Help us form a sql query that will return rows from the first table, only those whose flag in the second buffer is 0.
id = id_iz_pervoi_tablici. It is highly desirable that this be one sql query

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Melnikov, 2019-12-06
@immelnikoff

For MySQL 8.0:
Faster option (but it's not accurate):

SELECT * FROM `t1` WHERE EXISTS (SELECT * FROM `t2` WHERE `t1`.`id` = `t2`.`id_iz_pervoi_tablici` AND `flag`=0)

N
Never Ever, 2019-12-06
@Target1

SELECT * FROM `t1` 
  LEFT JOIN (SELECT * FROM `t2` WHERE `flag`=0)  AS `t2`
  ON `t1`.`id` = `t2`. `id_iz_pervoi_tablici`

S
Sergey c0re, 2019-12-07
@erge

SELECT t1.*
  FROM t1
  JOIN t2 ON t2.id_iz_pervoi_tablici = t1.id
  WHERE t2.flag = 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question