Skip to content
Advertisement

MYSQL JOINS, Got Stuck [closed]

good day! i’m in the middle of learning new thing in mysql, i need join 2 tables, table aktifitas_materi and table hari, i tried many join, but i think this is the closest one…

i’m using sql like this SELECT * FROM hari LEFT JOIN aktifitas_materi ON hari.id_hari = aktifitas_materi.id_hari WHERE id_materi = '1631246767791' AND id_user = '1631016713249' UNION ALL SELECT * FROM hari LEFT JOIN aktifitas_materi ON hari.id_hari = aktifitas_materi.id_hari

this is looks good, but when i change id_materi or id_user, the data still the same, Even though the data should be null, idk why this happen, please helppp, where’s the problem? 🙁

THE PROBLEM: I think my data didn’t get filter by where, because when i change id_materi or id_user, i got all the data…

EXPECTED RESULT

When i change id_materi or id_user to another number, i got data looks like this expected table


Schema (MySQL v5.7)

CREATE TABLE `hari` (
  `id_hari` int(11) NOT NULL,
  `hari` varchar(255) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `hari`
--

INSERT INTO `hari` (`id_hari`, `hari`, `timestamp`) VALUES
(1, 'Intro', '2021-09-10 03:42:52'),
(2, 'Hari 1', '2021-09-10 03:42:52'),
(3, 'Hari 2', '2021-09-10 03:42:52'),
(4, 'Hari 3', '2021-09-10 03:42:52'),
(5, 'Hari 4', '2021-09-10 03:42:52'),
(6, 'Hari 5', '2021-09-10 03:42:52'),
(7, 'Hari 6', '2021-09-10 03:42:52'),
(8, 'Hari 7', '2021-09-10 03:42:52');

CREATE TABLE `aktifitas_materi` (
  `id_aktifitas_materi` varchar(255) NOT NULL,
  `id_materi` varchar(255) NOT NULL,
  `id_hari` int(11) NOT NULL,
  `id_user` varchar(255) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `aktifitas_materi`
--

INSERT INTO `aktifitas_materi` (`id_aktifitas_materi`, `id_materi`, `id_hari`, `id_user`, `timestamp`) VALUES
('123213213', '23234234', 1, '123213213', '2021-09-10 11:01:13'),
('1631271674591', '1631246767791', 1, '1631016713249', '2021-09-10 11:01:13');

Query #1

SELECT * FROM `hari` LEFT JOIN aktifitas_materi ON hari.id_hari = aktifitas_materi.id_hari WHERE id_materi = '1631246767791' AND id_user = '1631016713249' UNION SELECT * FROM hari LEFT JOIN aktifitas_materi ON hari.id_hari = aktifitas_materi.id_hari;
id_hari hari timestamp id_aktifitas_materi id_materi id_user
1 Intro 2021-09-10 11:01:13 1631271674591 1631246767791 1631016713249
1 Intro 2021-09-10 11:01:13 123213213 23234234 123213213
Hari 1
Hari 2
Hari 3
Hari 4
Hari 5
Hari 6
Hari 7

View on DB Fiddle

Advertisement

Answer

I GOT MY ANSWER!!!

changed my sql to select * from hari left join (select * from aktifitas_materi where id_materi = '' and id_user = '') tm on hari.id_hari = tm.id_hari

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement