[英]Querying multiple tables in MySQL
I am trying to fetch data from multiple tables depending on what is selected in a dropdown menu. 我试图根据下拉菜单中选择的内容从多个表中获取数据。 My dropdown menu consists of a list of ID's (001, 002, etc). 我的下拉菜单包含ID的列表(001、002等)。
Once a user selects one of them, I am using AJAX to dynamically fetch data depending on what was selected. 用户选择其中一个后,我将使用AJAX根据选择的内容动态获取数据。 I was able to fetch a single value depending on what was selected but having problems when multiple tables are involved. 我能够根据选择的内容获取单个值,但是当涉及多个表时遇到问题。
My tables are set up like this: 我的表是这样设置的:
Inventory table: 库存表:
inven_ID (primary)
cost
description
Order table: 订单表:
order_ID(primary)
orderdesc
Sale table: 销售表:
inven_ID
order_ID
quantity
primary(inven_ID,order_ID)
My query is as follows: 我的查询如下:
$QRY = "SELECT
inven_ID,
order_ID,
cost,
description
FROM
Inventory,
Order,
Sale
WHERE Inventory.inven_ID = Sale.inven_id
AND Sale.order_ID = Order.order_ID
AND Order.order_ID ='".$q."'";
The $q
represents the value from the dropdown menu (which I checked is valid). $q
代表下拉菜单中的值(我检查过有效)。 I am getting the error Column ' inven_ID
' in field list is ambiguous. 我在字段列表中收到错误列' inven_ID
'的含义不明确。 Basically, when they select some order id from the drop down (say 001), it looks for order_ID
in my Order table, and fetches the inven_ID/cost/description of that particular order ID. 基本上,当他们从下拉菜单(例如001)中选择某个订单ID时,它将在我的“订单”表中查找order_ID
,并获取该特定订单ID的inven_ID / cost / description。
Eg. 例如。 if someone ordered parts xy, yz, xyz for cost 10,20,30. 如果某人订购零件xy,yz,xyz的成本为10、20、30。
Selecting 001 would bring up:
001 xy 10
001 yz 20
001 xyz 30
I think I am not joining tables properly since the error says its ambiguous. 我认为我没有正确地连接表,因为错误表明其模棱两可。
Any help on this? 有什么帮助吗?
edit: yes that fixed the problem, quite obvious that I did not catch it. 编辑:是的,解决了这个问题,很明显我没有抓住它。
In the column list of your select, you just need to specify which inven_ID you want to retrieve. 在您选择的列列表中,只需指定要检索的inven_ID。 For instance: 例如:
SELECT Inventory.inven_ID, ...
The error is pretty obvious. 错误非常明显。 inven_ID is ambiguous because you have it in Sale
and in Inventory
. inven_ID含糊不清,因为您在Sale
和Inventory
都有它。 Use a specifier like Sale.inven_ID
or Inventory.inven_ID
. 使用类似Sale.inven_ID
或Inventory.inven_ID
的说明符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.