azem loka
azem loka

Reputation: 13

How to use union just once with where command?

I want to use a query in php with where command where notifikasi = '1' in the 2 tables, but I only see 1 table that gets where notifikasi = '1', how can I make both my tables get where notifikasi = '1'?

SELECT a.id_skd as idab,a.nl as nl1, a.aa as aa1,a.jp as jp1, a.syarat_lampiran as syarat_lampiran1,a.riwayat_kelola as riwayat_kelola1,a.notifikasi as notifikasi1 
FROM skd a
UNION ALL 
SELECT b.id_skb as idab, b.nl as nl1, b.aa as aa1, b.jp as jp1, b.syarat_lampiran as syarat_lampiran1,b.riwayat_kelola as riwayat_kelola1, b.notifikasi as notifikasi1 
FROM skb b  
WHERE notifikasi='1' 
ORDER BY riwayat_kelola1 DESC 
LIMIT 1

Upvotes: 0

Views: 31

Answers (1)

Barmar
Barmar

Reputation: 780724

You have to repeat the WHERE clause, it only applies to one SELECT query, not the whole UNION.

SELECT a.id_skd as idab,a.nl as nl1, a.aa as aa1,a.jp as jp1, a.syarat_lampiran as syarat_lampiran1,a.riwayat_kelola as riwayat_kelola1,a.notifikasi as notifikasi1 
FROM skd a
WHERE notifikasi = '1'
UNION ALL 
SELECT b.id_skb as idab, b.nl as nl1, b.aa as aa1, b.jp as jp1, b.syarat_lampiran as syarat_lampiran1,b.riwayat_kelola as riwayat_kelola1, b.notifikasi as notifikasi1 
FROM skb b  
WHERE notifikasi='1' 
ORDER BY riwayat_kelola1 DESC 
LIMIT 1

Upvotes: 1

Related Questions