MySQL 如何在 PHPmyAdmin 中进行表连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8687627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:12:42  来源:igfitidea点击:

How to make Table Joins in PHPmyAdmin

mysqlsqlphpmyadmin

提问by Mark

I have 2 Tables in phpmyadmin that need joining

我在 phpmyadmin 中有 2 个表需要加入

tracklistingis my one and catelogueis the other, and are saved as innodb

tracklisting是我的一个catelogue是另一个,并保存为innodb

They both have a column CAT.NOand would like it to be joined on this column. In catelogue it is the primary and in tracklisting it's indexed

他们都有一个专栏,CAT.NO并希望将它加入到这个专栏中。在目录中它是主要的,在曲目列表中它被编入索引

catelogueis my parent and tracklistingwould be the child as it doesn't have info for every record in catelogue. I believe this would be correct unless I'm wrong

catelogue是我的父母并且tracklisting将是孩子,因为它没有目录中每条记录的信息。我相信这是正确的,除非我错了

How do I do this so that when I query on a column in tracklistingit only brings up the matches for 'catelogue' because I want to know what album it's on and not my entire 60000+ catelogue

我该怎么做,以便当我查询其中的列时tracklisting,只会显示“目录”的匹配项,因为我想知道它在哪个专辑上,而不是我的整个 60000+ 目录

Can this be done with phpmyadmin's interface or is this a sql statement

这可以用phpmyadmin的界面来完成还是这是一个sql语句

Many thanks

非常感谢

EDIT:

编辑:

This was the code that worked

这是有效的代码

SELECT *
FROM tracklisting
INNER JOIN catelogue ON catelogue.`CAT NO.` = tracklisting.`TRACKLISTING CAT NO.`
WHERE tracklisting.`ARTIST` LIKE 'placebo'

Thanks to everyone that helped out

感谢所有帮助过的人

回答by Starx

I dont know if this can be done with the interface, but with sql

我不知道这是否可以用接口来完成,但用sql

SELECT * 
FROM 
  tracklisting t 
  INNER JOIN catelouge c on c.catno=t.catno 
WHERE t.id = 1

回答by Ben Lee

You can query with a LEFT JOIN:

您可以使用 LEFT JOIN 进行查询:

SELECT * FROM tracklisting LEFT JOIN catelogue ON tracklisting.`CAT.NO` = catelogue.`CAT.NO` WHERE tracklisting.`<id-field>` = <id-value>`

http://dev.mysql.com/doc/refman/5.0/en/join.html

http://dev.mysql.com/doc/refman/5.0/en/join.html

回答by juergen d

Assuming that your tracklisting has an id and you want to query with it:

假设您的曲目列表有一个 id 并且您想用它查询:

select * from tracklisting t
inner join catelogue c on c.cat.no = t.cat.no
where t.id = 1

回答by davidethell

The actual join takes place in a SQL statement although with certain storage types like InnoDB you can also create foreign key references that enforce the relationship at the database level so that your inserts require the proper records to be in place and your deletes are restricted if child records exist.

实际连接发生在 SQL 语句中,尽管对于 InnoDB 等某些存储类型,您还可以创建外键引用来强制数据库级别的关系,以便您的插入需要适当的记录到位,并且您的删除受到限制记录存在。

Here is one way of doing the join syntax for your SQL query:

以下是为 SQL 查询执行连接语法的一种方法:

SELECT t.* FROM tracklisting t, catalogue c
WHERE `t.CAT.NO` = `c.CAT.NO`

EDIT: Hereis a link to a tutorial for creating foreign keys in phpMyAdmin.

编辑:是在 phpMyAdmin 中创建外键的教程的链接。