Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query, where = value of another table

Tags:

sql

sqlite

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,

like image 441
Rodolfo Matos Avatar asked May 31 '12 08:05

Rodolfo Matos


People also ask

How do I find data from another table in SQL?

In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.


2 Answers

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.

like image 185
Pranay Rana Avatar answered Sep 28 '22 05:09

Pranay Rana


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

like image 45
t-clausen.dk Avatar answered Sep 28 '22 07:09

t-clausen.dk