简体   繁体   English

结合MySql中多个表的结果

[英]Combining results from multiple tables in MySql

I have two tables. 我有两张桌子。 One contains members and the other members dogs . 一个包含members和其他成员的dogs

The dogs table contains a membership number which is what links it to the `members' table. dogs表包含一个会员编号,它将其链接到`members'表。

The members table contains all of the address information. members表包含所有地址信息。

Every week, I have to select new paid registrations and send out cards. 每周,我必须选择新的付费注册并发送卡片。 So I need to combine details from the dog table with information from member and have it all on one row. 因此,我需要将来自dog表的详细信息与来自member信息结合起来,并将其全部放在一行上。 Then I can export it and send it to our card printer. 然后我可以将其导出并发送到我们的卡片打印机。

What is the best way to do this? 做这个的最好方式是什么? I have been playing with JOINS and just can't quite get it to work. 我一直在玩JOINS,但却无法让它发挥作用。

Exporting to the printer I leave to you, but the JOIN is rudimentary. 导出到打印机我留给你,但JOIN是基本的。 Replace membership_number with the correct column name. 使用正确的列名替换membership_number

SELECT
  members.*,
  dogs.*
FROM
  members
  JOIN dogs ON members.membership_number = dogs.membership_number

The above is brief, but in practice it is usually better to explicitly name all columns in the SELECT list, especially when you have the same named column in both tables of the join. 以上是简短的,但实际上通常最好明确命名SELECT列表中的所有列,尤其是当您在连接的两个表中具有相同的命名列时。

SELECT
  members.membership_number,
  members.address,
  members.state,
  members.someothercolumn,
  dogs.name,
  dogs.breed,
  dogs.someotherdogcolumn
FROM
  members
  JOIN dogs ON members.membership_number = dogs.membership_number

The MySQL JOIN syntax reference has many examples which can guide you in future queries. MySQL JOIN语法参考有许多示例,可以指导您将来的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM