首页 > 解决方案 > 多表选择

问题描述

我不知道这是否可能,但这就是我想做的。我有三张桌子

账户

id | username | pass

汽车

id | plate | id_ac | kilometers // in the id_ac I add manual the id of the car's owner.

旅行

id | plate | places | date | active

现在我想在用户登录时查看他的车正在使用的行程。所以我想

SELECT from Trips the plate, places, date WHERE active=0 AND id= ? (the logged user's id)

但是 table trips 没有车主的 ID。所以我想以某种方式选择登录用户拥有的汽车的旅行表的值。

知道我该怎么做吗?

标签: phpsql

解决方案


你应该看看表连接。这看起来就像您正在寻找的内容:

SELECT t.plate, t.places, t.date FROM Trips as t JOIN Cars as c ON t.plate = c.plate WHERE t.active = 0 AND c.id_ac = ?

看看这个。这是解释表连接的一种很好的方式。 http://www.sql-join.com/sql-join-types

我没有进入数据库架构和更深层次的东西,但它可以帮助你完成手头的任务


推荐阅读