1

I have two table, product and productimages

product table;

|----------------Table:product-|
|ID | tr_productname           |
|---|--------------------------|
| 1 | Computer                 |
| 2 | Telephone                |
| 3 | Television               |
| 4 | Mouse                    |
|------------------------------| 

productimages table;

|-----------Table:productimages-|
|ID | productid  |  imageurl    |
|---|------------|--------------|
| 1 |     1      | computer.jpg |
| 2 |     1      | computer2.jpg|
| 3 |     2      | telephone.jpg|
| 4 |     2      | x.jpg        |
| 5 |     2      | abc.jpg      |
| 6 |     3      | tv.jpg       |
|-------------------------------| 

I want list products with image but if image does'nt seem to record. I want list all product.

MyQuery;

SELECT product.id, productcode, tr_productname, brand, imageurl 
FROM product 
LEFT JOIN productimages 
ON product.id = productimages.productid 
GROUP BY productimages.productid 
ORDER BY product.id DESC 

Result;

|-----------------------------------RESULT----|
|ID | tr_productname           |    imageurl  |
|---|--------------------------|--------------|
| 1 | Computer                 | computer.jpg |
| 2 | Telephone                | telephone.jpg|
| 3 | Television               | tv.jpg       |
|------------------------------|--------------| 

But no product with "4" ID

2
  • I guess you are looking for all images for per product and if yes try group_concat(imageurl) as imageurl Commented Jun 25, 2014 at 11:32
  • I try join and in select but didn't work. It work but 4th record not seem. Need productimages record for seem a product. I want get list all product. Didn't work group_concat(imageurl) as imageurl Commented Jun 25, 2014 at 11:37

4 Answers 4

1

Try to change GROUP BY productimages.productid to product.id like this

SELECT product.id, productcode, tr_productname, brand, imageurl 
FROM product 
LEFT JOIN productimages 
ON product.id = productimages.productid 
GROUP BY product.id 
ORDER BY product.id DESC 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this ..

SELECT product.id, productcode, tr_productname, brand, imageurl 
FROM product 
LEFT JOIN productimages 
ON product.id = productimages.productid 
GROUP BY product.id 
ORDER BY product.id DESC 

Comments

0

When you are using group by, the value involves in group by only shown. In your case you have grouped based on productimages table, in this table 4 value is not present. So use product.id in group by.

SELECT product.id, productcode, tr_productname, brand, imageurl 
FROM product 
LEFT JOIN productimages 
ON product.id = productimages.productid 
GROUP BY product.id 
ORDER BY product.id DESC 

Comments

0
SELECT product.id, productcode, tr_productname, brand, imageurl 
FROM product 
LEFT JOIN productimages 
ON product.id = productimages.productid 
GROUP BY product.id 
ORDER BY product.id DESC 

1 Comment

A code-only or query-only answer is not usually a "quality" answer. Please provide some explanation as to why you believe that this answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.