amindotb amindotb - 6 months ago 12
MySQL Question

mysql query search for two conditions on two table

I have two table, #1: row(int),id(int),type(int)
and next one #2: row(int),id(int),visit(int)

I want to set a query to select all of items in table #2 which has type=1 in table #1.

How to do that ?

Answer

Here you go :

SEELCT row, id, visit FROM table2
INNER JOIN table1 USING(id)
WHERE table1.type = 1

Let me explain the INNER JOIN table1 USING(id) : The JOIN means you collapse table rows of the table1 to the table2, INNER excludes the rows where no id from the table1 match the id from the table2, and finally USING(id) ask the engine to use the id column to match rows.