Jump to content

Left join, if table empty only one result [BEGINNER] EMERGENCY


webbhelp
Go to solution Solved by Barand,

Recommended Posts

Hi!

 

I am getting crazy about this problem. I really need your help because the deadline is today and this is the last thing to do.

 

I got a lot of tables here:

 

products (all products)

products_lang (All translation to the product, title and description)

products_img (All the images to each product)

customer(All customer, name, email etc.)

customer_products(All products the customer bought)

customer_img (Every product each customer buy; there is a possibility to upload an image to that product, an attached image) //THIS ONE IS A BITCH!

 

Okay! I wan't to select all the products (`products`) with the ID's from the table `customer_products` WHERE the customer_products.customer = 3.

Then I select the products the customer has bought. and with that I want the products images, AND the images the customer uploaded to each image.

The problem here is that I only receive one row when I should get 4 rows.AND I don't get the C_imgurl (customer_img.imgurl).

 

BUT if I remove:

LEFT JOIN `customer_img` ON `customer_img`.`product` = `customer_products`.`product`

Then it works; I get the 4 rows.

Good to know maybe, is that the customer_img is empty, that is why I use left JOIN

SELECT 
`products`.`id`,
`products`.`outprice`AS price,
`products`.`vat`,
`products`.`imgupload`, 
`products_lang`.`title`,
`products_img`.`imgurl`,
count(customer_img.id) as countimg,
`customer_img`.`imgurl` AS c_imgurl,
`categories`.`category` 
FROM (`products`)
JOIN `customer_products`ON `customer_products`.`product` = `products`.`id` 
LEFT JOIN `customer_img` ON `customer_img`.`product` = `customer_products`.`product`
LEFT JOIN `products_lang` ON `products_lang`.`product` = `products`.`id` 
LEFT JOIN `products_img` ON `products_img`.`id` = `products`.`firstimg` 
JOIN `categories` ON `categories`.`id` = `products`.`category` 
WHERE `customer_products`.`customer` = 3 AND `products_lang`.`language` = '3'

Please help me with this!

 

Thanks!

Ask if you don't understand!

Link to comment
Share on other sites

I see you have a WHERE condition on a LEFT JOINed table. It needs to be in the JOIN condition

 

Try

SELECT 
`products`.`id`,
`products`.`outprice`AS price,
`products`.`vat`,
`products`.`imgupload`, 
`products_lang`.`title`,
`products_img`.`imgurl`,
count(customer_img.id) as countimg,
`customer_img`.`imgurl` AS c_imgurl,
`categories`.`category` 
FROM (`products`)
JOIN `customer_products`ON `customer_products`.`product` = `products`.`id` 
LEFT JOIN `customer_img` ON `customer_img`.`product` = `customer_products`.`product`
LEFT JOIN `products_lang` ON `products_lang`.`product` = `products`.`id` AND `products_lang`.`language` = '3'
LEFT JOIN `products_img` ON `products_img`.`id` = `products`.`firstimg` 
JOIN `categories` ON `categories`.`id` = `products`.`category` 
WHERE `customer_products`.`customer` = 3 
Link to comment
Share on other sites

Now my result look like this:

 

 

 

1157 1000 12 1 llllllllllllllllll sverige2.png 0 NULL Apple

That is one row, null is the imgurl. and it will be null because there is no images. so it is correct.

BUT the would be 3 row more.

Because in customer_products there is: 4 rows, with same customer(id), and different product(id).

 

so I don't understand why the 3 other result doesn't show?

Link to comment
Share on other sites

SELECT 
`products`.`id`,
`products`.`outprice`AS price,
`products`.`vat`,
`products`.`imgupload`, 
`products_lang`.`title`,
`products_img`.`imgurl`,
count(customer_img.id) as countimg,
`customer_img`.`imgurl` AS c_imgurl,
`categories`.`category` 
FROM (`products`)
JOIN `customer_products`ON `customer_products`.`product` = `products`.`id` 
LEFT JOIN `customer_img` ON `customer_img`.`product` = `customer_products`.`product`
LEFT JOIN `products_lang` ON `products_lang`.`product` = `products`.`id` AND `products_lang`.`language` = '3'
LEFT JOIN `products_img` ON `products_img`.`id` = `products`.`firstimg` 
JOIN `categories` ON `categories`.`id` = `products`.`category` 
WHERE `customer_products`.`customer` = 3 

JOIN `customer_products`ON `customer_products`.`product` = `products`.`id`

//using this to only fetch the products where customers_productds -> productid is the same as the product.id; this works I get the product data

 

 LEFT JOIN `customer_img` ON `customer_img`.`product` = `customer_products`.`product`

//Getting all the images connected to the customer_products. IF I remove this line, all the 4 products is received

//If this line is in use I get:

 

1157 1000 12 1 llllllllllllllllll sverige2.png 0 NULL

Apple

 

But I am suppose to get 3 more rows.

---

 

LEFT JOIN `products_lang` ON `products_lang`.`product` = `products`.`id` AND `products_lang`.`language` = '3'

//Selecting all translations to the product., this works I get the title.

 

LEFT JOIN `products_img` ON `products_img`.`id` = `products`.`firstimg`

//Selecting the image to the product, this works, I get the image

Link to comment
Share on other sites

  • Solution

Try removing

count(customer_img.id) as countimg,

You have an aggregation without GROUP BY.

But, if you add a group by then you will get only a single row per product even if there are multiple images

 

edit :

or add GROUP BY products.id

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.