MySQL非常简单的连接示例请求子表

I keep falling back into questions with MySQL joining. And I would like to request a very simple example I could use to continue my journey of understanding learning the MySQL syntax.

Let's say I got the following table's

test_testtable

  • testtable_id
  • testtable_name
  • testtable_user
  • testtable_option
  • testtable_textfield

test_testlink

  • testlink_id
  • testlink_link
  • testlink_address

test_address

  • address_id
  • address_name
  • address_phone
  • address_email
  • address_street
  • address_city
  • address_zip

I would like to make a selection like :

SELECT * (lets say I would define the fields) FROM `test_testable` 
JOIN `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
AND
JOIN `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5

Hence the linking structure is like:

test_testtable.testtable_id = leading

table test_testlink is a table to link the table test_testtable and test_address

And linking table test_testlink uses the field testlink_link to link to the table test_testtable, and uses the field testlink_address to link to the table test_address

This does not work. FOR ME.. Since I continuously seem to fail of catching the correct syntax logic.

So I hope that someone could give me a small example of how to correctly implement such a simple yet critical query!

TIAD!!

A general approach :

SELECT table1.* FROM table1
JOIN table2 ON table2.id_table1 = table1.id
JOIN table3 ON table3.id_table2 = table2.id
WHERE table1.id = 10

For your purpose :

SELECT * (lets say I would define the fields) FROM `test_testable` 
JOIN `test_testlink` ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
JOIN `test_address` ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
WHERE `user_id` = 5

Please read the reference

You are using wrong syntax. You should mention which tables to join first then based on which fields.

SELECT * (lets say I would define the fields) FROM `test_testable` 
INNER JOIN test_testlink 
ON `test_testtable`.`testtable_id` = `test_testlink`.`testlink_link`
INNER JOIN `test_address` 
ON `test_testlink`.`testlink_addres` = `test_address`.`address_id`
AND `test_testtable`.`user_id` = 5
select * from testlink JOIN testtable ON testlink.tableid = testtable.ID
                       JOIN testaddress ON testlink.addressid = testaddress.ID
WHERE testtable.ID = 5