marc_s
marc_s

Reputation: 1047

Easiest way to select from 2 tables, with where condition in second

I'm selecting all entries from first table which have certain category in second one. Those 2 are connected through ID, which is the same in both tables.

I'm new at UNION and JOIN, so I'm wondering if I can do this without using those? Example:

SELECT * 
  FROM entries, 
       categories 
 WHERE entries.id = categories.id 
       AND categories.category = 'default'

Upvotes: 1

Views: 57

Answers (3)

fmgonzalez
fmgonzalez

Reputation: 823

The query is correct. Another way:

SELECT * 
  FROM entries 
  JOIN catetories ON entries.id = categories.id
 WHERE categories.category = 'default'; 

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Try like this

  SELECT * 
  FROM entries INNER Join categories ON entries.id = categories.id 
  WHERE categories.category = 'default'

Upvotes: 1

Git Psuh
Git Psuh

Reputation: 322

This would work. You might as well type :

SELECT your fields FROM
entries AS E INNER JOIN categories as C USING (id)
WHERE C.category = 'default'

Upvotes: 2

Related Questions