0

This is my current code

SELECT * FROM pn_queue WHERE email = '2'

Now i want to extend is with an "AND" wich is in another table. Like this:

SELECT * FROM pn_queue WHERE email = '2' AND FROM pn_cats WHERE cat = '2'

How can i make it? I will be thankfull for any ideas or solutions.

  • Is there any dependencies between this tables ? If yes then use **JOIN** as mention by @Jean-François Savard Else use **UNION ALL** – Nagendra Nigade Feb 02 '15 at 04:08
  • What do you mean with dependencies? I have a table with subscribers (id, name, email, code and so on) and a table with categries (id, code, cat) now i want to add a mail to a queue when something happens. To look wich should become a mail i want to look if a sub has a special flag and if he is in a special category. So the select must be over to tables. – wassereimer Feb 02 '15 at 04:32

1 Answers1

0

You need to use a Join

select * from pn_queue a
  join pn_cats b on a.pn_cats_id = b.id -- Change this condition to whatever should match on both table.
where b.cat = '2'
  and a.email = '2'

Not sure if the join condition is actually right as you don't give enought information, but just modify the condition a.pn_cast_id = b.id if it ain't.

Edit : Just realized in your example cat and email are both equals to 2 so if it should be the match value then the correct query would simply be :

select * from pn_queue a
  join pn_cats b on b.cat = a.email

For more information on SQL Join, you should check this thread What is the difference between "INNER JOIN" and "OUTER JOIN"?. It explain the difference between all possible joins with diagrams/examples etc.

Community
  • 1
  • 1
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • Thank you for your answer. Sorry for the bad example. Do you know a side with a good description for the needed function? The two single select will be 1) SELECT * FROM pn_subs WHERE status = '1' ---- 2) SELECT * FROM pn_cats WHERE cat = '50' But the second one should be an AND to the first one. So that i become only data from the first table when on the first table status=1 and on the second table cat=50. – wassereimer Feb 02 '15 at 04:42
  • `select * from pn_subs where status='1' union all select * from pn_cats where cat='50';` – Nagendra Nigade Feb 02 '15 at 04:48
  • You guys have sended me to the right thoughts and ideas! union all don´t work in my case. join is the right way. The code for me must be: SELECT * FROM pn_subs JOIN pn_cats ON pn_subs.code = pn_cats.code WHERE pn_subs.status = 1 AND pn_cats.cat = '2' I think it would be easier for you guys next time when i use sqlfiddle or something else and give you more code and infrmations. Sorry for my fault. Thank you very much! – wassereimer Feb 02 '15 at 10:56