I have two tables: one for transacctions and one for bills. It could be aprpoved transactions that generates a bill. There could be NOT approved transactions that NOT generates a bill.
The problem is that I would like to print the info of both tables, but is not possible to see if there will be (or not) a bill.
Some representatives fields are from:
Transactions: service_bill, auth_number, UserId ...
Bill: UserId, ...
When I do the query:
SELECT *
FROM transactions, bill
WHERE MONTH(date) = '09'
AND
YEAR(date) = 2011
(...)
AND
bill.userId = transactions.userId
(...)
ORDER BY id_transaction
If I use that query, I will print the information that matchs the “userId”, but the problem is that it WONT print the transactions that does not have bills -because, you don´t have an userId to match-
If I relax the where, it will print several times the same information :/
At the end, I want to print all the information -without repetitions- and in the NOT generated bills, when the transactions is displayed the bill info is NULL or something like that.
Any idea to solve this?
You want to use a LEFT JOIN in this case. This returns all rows from the first table along with any matches found in the second table. Any unmatched rows will return NULL values for the columns of the second table.