0

I have 4 tables: table1, table2, table3, table4. table1 contains id values related to all other tables (2,3,4) and I would like to connect all of them to have all data in one set, I think it's simple relation.

I tried this, but I don't know why it doesn't work:

SELECT * FROM 'table1'
JOIN 'table2' ON 'table1.id_forT2'='table2.id'
JOIN 'table3' ON 'table1.id_forT3'='table3.id'
JOIN 'table4' ON 'table1.id_forT4'='table4.id'
WHERE 'table1.id' = 30

When I left only one join it works fine, with 2 or 3 joins it returns an error. How to do that? Thanks in advance

cccp
  • 33
  • 4
  • 2
    Possible duplicate of [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – Siyual Jul 20 '17 at 21:13

2 Answers2

0

I think it's just that your ticks aren't in the right place. And is this MySQL? Because then you want back-ticks instead of single quotes.

SELECT * FROM `table1`
JOIN `table2` ON `table1`.id_forT2=`table2`.id
JOIN `table3` ON `table1`.id_forT3=`table3`.id
JOIN `table4` ON `table1`.id_forT4=`table4`.id
WHERE `table1`.id = 30
Ryan B.
  • 3,575
  • 2
  • 20
  • 26
  • Of course, you are right! My ticks weren't in the right place, now it works fine! Thanks a lot! – cccp Jul 27 '17 at 17:31
0

It might be because of two reassons:

  1. Quotes/Backtips: Maybe you aren't using this properly
  2. Use LEFT JOIN instead of JOIN: Perhaps one table has no data for the relation

You should specify if you're using mySQL, postgreSQL or other.

Daniel Batalla
  • 1,184
  • 1
  • 11
  • 22