doufu5747 2015-01-22 09:30
浏览 13
已采纳

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!!

  • 写回答

3条回答 默认 最新

  • douqiang5163 2015-01-22 09:40
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助