13

I want to make a query that simply makes this, this may sound really dumb but i made a lot of research and couldn't understand nothing.

Imagine that i have two tables (table1 and table2) and two columns (table1.column1 and table2.column2).

What i want to make is basically this:

SELECT column1 FROM table1 where table2.column2 = '0'

I don't know if this is possible.

Thanks in advance,

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Rodolfo Matos
  • 303
  • 1
  • 3
  • 11

3 Answers3

42

You need to apply join between two talbes and than you can apply your where clause will do work for you

select column1 from table1 
   inner join table2 on table1.column = table2.column
   where table2.columne=0

for join info you can see this

Reading this original article on The Code Project will help you a lot: Visual Representation of SQL Joins.

alt text

Find original one at: Difference between JOIN and OUTER JOIN in MySQL.

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
7
SELECT column1 FROM table1 t1
where exists (select 1 from table2 t2 
    where t1.id = t2.table1_id and t2.column2 = '0')

assuming table1_id in table2 is a foreign key refering to id of table1 which is the primary key

t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
2

You don't have any kind of natural join between two tables.

You're asking for

Select Houses.DoorColour from Houses, Cars where Cars.AreFourWheelDrive = '1'

You'd need to think about why you're selecting anything from the first table, there must be a shared piece of information between tables 1 and 2 otherwise a join is pointless and probably dangerous.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Jeff Watkins
  • 6,343
  • 16
  • 19