0

Will try to explain as best i can :)

I have 2 tables

Cloths tables (many) to Question Tables (one)

I want to show questions to a cloth item. Which works fine

What i am trying to understand, is that if a cloth item is removed from the database, the question will not sure up

as i have this as my sql command

"SELECT contact.id, contact.name, contact.email, contact.comments,contact.type, contact.stockid, contact.date ,
                      stock.stockid, stock.name ,stock.mainimage,  stock.price FROM contact,stock
                      where contact.stockid = stock.stockid
                      and contact.type ='ques'"

This will display the question related to stock item, what i am trying to get my brain around, how would i do it for example

If no stock.stock id, display question, but with a header, "this question is for a stock item that has been removed.

Any guidance would be great.

Many thanks

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

1

You need a LEFT JOIN for this. Try the following query:

"SELECT contact.id, contact.name, contact.email, contact.comments,
    contact.type, contact.stockid, contact.date
    stock.name, stock.mainimage,  stock.price
FROM contact
LEFT JOIN stock ON (stock.stockid = contact.stockid)
WHERE contact.type ='ques'"

When you are fetching the rows, if name, mainimage or price fields are NULL, that means there are no stocks for that contact.

mesutozer
  • 2,839
  • 1
  • 12
  • 13
0
FROM `contact`

LEFT JOIN `stock` ON `stock`.`id` = `contact`.`stockid`
laalto
  • 150,114
  • 66
  • 286
  • 303
rumaviio
  • 1
  • 1