Home > Back-end >  MySQL: Trouble trying to get my SQL statement to work when condition is true but can also accept fal
MySQL: Trouble trying to get my SQL statement to work when condition is true but can also accept fal

Time:09-22

I have the following statement:

SELECT tableA.*, tableB.* FROM tableA, tableB WHERE tableA.userUUID = ? AND tableB.uuid=tableA.tableBUUID

So when a record in table A has a valid tableBUUID and the tableBUUID exists in tableB then the query runs file. But when I removed the tableBUUID from table B without removing it from the record in tableA then the query returns nothing (Which is obviously expected)

I am wondering how I can change the query so that even if the tableBUUID does not match anything, the query will still return what it can even if its nothing/ blank columns from tableB?

Thanks all

CodePudding user response:

You can achieve this via a LEFT JOIN

In the syntax you posted above in your question, you're essentially doing a INNER JOIN by comma separating your tables. This is the old syntax, moving forward you should explicitly write INNER JOIN like so:

SELECT * FROM tableA a INNER JOIN tableB b ON a.column_key = b.column_key

Read more about in this previous question:
What's the difference between comma separated joins and join on syntax in MySQL?

  • Related