Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS tabel1;
- CREATE TABLE IF NOT EXISTS tabel1 (
- id TINYINT(2) unsigned NOT NULL,
- nama VARCHAR(10)NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO tabel1
- VALUES
- (1,'data 1'),
- (2,'data 2'),
- (3,'data 3'),
- (4,'data 4'),
- (5,'data 5'),
- (6,'data 6'),
- (7,'data 7'),
- (8,'data 8'),
- (9,'data 9'),
- (10,'data 10');
- DROP TABLE IF EXISTS tabel2;
- CREATE TABLE IF NOT EXISTS tabel2 (
- tabel1_id TINYINT(2) unsigned NOT NULL,
- cat_id TINYINT(2)unsigned NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO tabel2
- VALUES
- (1,1),
- (1,2),
- (1,3),
- (2,1),
- (2,2),
- (3,1),
- (4,1),
- (4,3),
- (5,2),
- (6,2);
- DROP TABLE IF EXISTS cat;
- CREATE TABLE IF NOT EXISTS cat (
- id TINYINT(2) unsigned NOT NULL,
- nama VARCHAR(10)NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO tabel2
- VALUES
- (1,'cat 1'),
- (2,'cat 2'),
- (3,'cat 3');
- -- #1 QUERY
- SELECT
- a.id,
- a.nama
- FROM
- tabel1 a
- JOIN tabel2 b ON a.id = b.tabel1_id
- WHERE
- b.cat_id IN (1,2)
- GROUP BY
- a.id
- HAVING
- COUNT(a.id)=2
- -- #2 QUERY
- SELECT
- b.id,
- b.nama
- FROM
- tabel2 a
- JOIN tabel1 b ON b.id=a.tabel1_id
- GROUP BY
- a.tabel1_id
- HAVING
- SUM(IF(a.cat_id=1,1,0)) * SUM(IF(a.cat_id=2,1,0))=1
- -- #3 QUERY
- SELECT
- b.id,
- b.nama
- FROM
- tabel2 a
- JOIN tabel1 b ON b.id=a.tabel1_id
- JOIN tabel2 c ON a.tabel1_id=c.tabel1_id
- AND a.cat_id=1
- AND c.cat_id=2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement