Im trying to get a simple JOIN Query with mysql to work. I have two tables (see below).
My expected output are two Rows like you can see below. But on my localhost the output are 265 rows? i dont even have that much inputs so they all duplicates.
How can i get this to work?
See below what i have so far.
Thanks
Product Table (DB: products)
| id | created_time | user_hash | product_hash | title |
|----| ------------ | ---------- | ------------ |---------------- |
| 0 | 2021-01-01 | 6dfb81562c | 5877a110 | Product Title 0 | x
| 1 | 2021-01-01 | ca69a68665 | 5877a111 | Product Title 1 |
| 2 | 2021-01-01 | 6dfb81562c | 5877a112 | Product Title 2 | x
| 3 | 2021-01-01 | ca69a68665 | 5877a113 | Product Title 3 |
| 4 | 2021-01-01 | ca69a68665 | 5877a113 | Product Title 4 |
| 5 | 2021-01-01 | ca69a68665 | 5877a113 | Product Title 5 |
| 6 | 2021-01-01 | 0d9a7e91fc | 5877a113 | Product Title 6 |
Favourite Products Table (DB: favourites)
| id | created_time | user_hash | product_hash |
|----|--------------|------------|--------------|
| 0 | 2021-01-01 | 6dfb81562c | 5877a114 | x
| 1 | 2021-01-01 | 6dfb81562c | 5877a115 | x
| 2 | 2021-01-01 | 0d9a7e91fc | 5877a116 |
| 3 | 2021-01-01 | 0d9a7e91fc | 5877a117 |
| 4 | 2021-01-01 | a5dc8c1d74 | 5877a118 |
ExPeCted Output:
| id | created_time | user_hash | product_hash | title |
|----| ------------ | ---------- | ------------ |---------------- |
| 0 | 2021-01-01 | 6dfb81562c | 5877a110 | Product Title 0 | x
| 1 | 2021-01-01 | 6dfb81562c | 5877a112 | Product Title 2 | x
Actual Output:
| id | created_time | user_hash | product_hash | title |
|------| ------------ | ----------- | ------------ |-------|
| 0 | 2021-01-01 | 8978b81562c | 5877a110 | ... |
| 1 | 2021-01-01 | 8978891562c | 5877a112 | ... |
| 1 | 2021-01-01 | 6dfb81562c | 5877a115 | ... |
| 2 | 2021-01-01 | 0d9a7e91fc | 5877a116 | ... |
| 3 | 2021-01-01 | 0d9a7e91fc | 5877a117 | ... |
| 4 | 2021-01-01 | a5dc8c1d74 | 5877a118 | ... |
| 5 | 2021-01-01 | 8978891562c | 5877a112 | ... |
| 6 | 2021-01-01 | 6dfb81562c | 5877a115 | ... |
| 7 | 2021-01-01 | 0d9a7e91fc | 5877a116 | ... |
| ... | 2021-01-01 | 0d9a7e91fc | 5877a117 | ... |
| 265 | 2021-01-01 | a5dc8c1d74 | 5877a118 | ... |
My Query:
$stmt = $pdo->prepare("SELECT * FROM products INNER JOIN favourites ON products.user_hash = favourites.user_hash ");
$stmt->execute();
$i = 1;
while($row = $stmt->fetch()) {
echo '#'.$i. ' Row';
$i++
}