php - MySQL:如何根据另一个表中的信息从表中进行选择?

标签 php mysql sql database join

我有两张 table ...

门票

id | customer

客户

id | name

我需要从该票证的客户名为 John 的票证中选择 *。我尝试使用 JOIN下面查询但使用 mysqli_fetch_array() 时为了获取数据,它错误地返回了票证“客户”字段。它与票证“id”字段相同,没有任何意义。

SELECT * 
FROM tickets 
INNER JOIN customers ON tickets.customer = customer.id 
WHERE customer.name = 'John';

我怎样才能做到这一点?我觉得这一定在 Stack Overflow 上的某个地方,但我没有运气。

最佳答案

如果您的表像这样命名客户,只需删除 INNER 并按顺序写入您的表名称(将 s 添加到 customer):

select * from tickets join customers ON (tickets.customer = customers.id) where customers.name = 'John'

关于php - MySQL:如何根据另一个表中的信息从表中进行选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53056514/

相关文章:

SQL Server : does an CTE update work like a transaction?

php - groupBy 无法在 laravel 中的 eloquent 和查询构建器中正常工作

php - PostgreSQL 日期范围

php - 我想知道如何在我的 Controller (laravel)中不使用 faker 技术的情况下在我的数据库 Mysql5.7 中获取一些字段

php - 如何从多个表中高效地访问用户数据

MySQL,如何以这种格式获取日期范围 : YYYY-MM-DD HH

php - 从数组生成动态链接

mysql - 如何在 MySQL 中使用区间比较创建表之间的关系?

sql - 'All' 时的情况

mysql - 分组依据不返回 "0"