使用具有相同外键的两列
我有一个数据库,其中有 5 个表,但我有问题。在发货表中,有两列,来源和目的地,它们都是对路线表的外键引用,但是当我从发货表中选择一条记录时,它只会显示两者相同的路线名称。
这是代码:
SELECT fare, commission, driver,shipment._date, routename, vehiclenumber, productname, source, destination, routename,ownername
FROM route, shipment, product, vehicle,owner
WHERE vehicle.vehicleid = shipment.vehicle
AND shipment.source
and vehicle.owner=owner.ownerid
AND shipment.destination = route.routeid
AND shipment.product = product.productid
AND vehicle.vehiclenumber = 'nk-234'
ORDER BY _date
LIMIT 0 , 30
I have a database, which has 5 tables, but I have problem. In the shipment table, there are two columns, source and destination, which are both foreign key references to the route table, but when I select a record from the shipment table it will only show the same routename for both.
Here is the code:
SELECT fare, commission, driver,shipment._date, routename, vehiclenumber, productname, source, destination, routename,ownername
FROM route, shipment, product, vehicle,owner
WHERE vehicle.vehicleid = shipment.vehicle
AND shipment.source
and vehicle.owner=owner.ownerid
AND shipment.destination = route.routeid
AND shipment.product = product.productid
AND vehicle.vehiclenumber = 'nk-234'
ORDER BY _date
LIMIT 0 , 30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要将一个表中的一条记录(一次发货)连接到另一个表中的多个记录(两条路线),理想情况下,您最好对
route
表使用显式JOIN
两次(或更多,无论您需要多少链接)。这是对查询的快速修改,演示了用法。请特别注意
SELECT
中的两个routename
列以及到route
表的两个JOIN
FROM
:显然,这可能不完全符合您的需求,因为我不知道其他选定字段来自哪些表,以确保它们都被考虑在内......
To connect a record in one table (one shipment) to more than one record in another table (two routes) you would ideally use explicit
JOIN
s against theroute
table twice (or more, for however many links you happen to need).Here's a quick modification to your query that demonstrates usage. Pay particular attention to the two
routename
columns in theSELECT
and the twoJOIN
s to theroute
table in theFROM
:Obviously, this might not match your needs exactly, because I don't know what tables the other selected fields are from to be able to make sure they are all accounted for...
这是因为您从发货中的一行中选择字段,为了真正从外键中获得任何效果,您需要使用一些连接
http://dev.mysql.com/doc/refman/5.5/en/join.html
This is because you select the fields from one row in shipment, in orer to actually get any effect from your foreign keys you need to use a few joins
http://dev.mysql.com/doc/refman/5.5/en/join.html
使用旧式(非首选)符号,您可以这样写:
我必须猜测哪个表包含票价、佣金和司机信息;我以为它们都在发货表中。
标准 SQL 中并不严格允许以下划线开头的列名,但大多数 DBMS 允许这样做。但它看起来很丑;为什么不使用“ship_date”作为列名?
最好使用显式连接表示法,尤其是因为忘记连接条件的可能性较小:
Using the old-style (non-preferred) notation, you can write:
I had to guess which table(s) contain the fare, commission and driver information; I assumed they are all in the shipment table.
Starting a column name with an underscore is not strictly allowed in standard SQL, though most DBMS do allow it. It looks ugly, though; why not 'ship_date' as the column name?
It is better to use the explicit join notation, not least because there is less chance of forgetting a join condition: