I am trying to run through a program to check if vouchers are valid within a vouchers table.
There are some simple validation aspect such as whether the voucher has passed expiry date or not, but also, some vouchers might be conditional based upon a customer buying specific products by brand, or supplier.
So, I have table of vouchers
id | brand | supplier | value etc
a table of brands
id | name etc
and a table of suppliers
id | name etc
If a voucher is not specific to a brand or supplier, then the value in the vouchers tale is 0
I have tried this Sql query inner joining the tables, but I get no results, I am assuming this is because the voucher code (id) I am trying to use is not specific to a brand or supplier?
SELECT *, brands.name as brandName, suppliers.name as supplierName
FROM `vouchers`
INNER JOIN brands on vouchers.brand = brands.id
INNER JOIN suppliers on vouchers.supplier = suppliers.id
WHERE vouchers.id= '" & voucherCode & "'
How would I write this so that the joining of the table brands is conditional upon the value brand in the table vouchers is greater than 0 (and then simiarly for suppliers)
I have had a look on here and see several other questions posted, but I am afraid I don’t fully understand the answers to e able to relate them to my situation.
Using MySQL database by the way 🙂
Why don’t you just do a
LEFT JOINinstead like this:This will return all the
vouchersno matter if they havebrandsorsuppliers