简体   繁体   English

MySQL Query返回其他结果

[英]MySQL Query is returning additional results

I'm trying to write a query that returns lastName from customers table, bidAmount and bidTime from bids table and productName from product table on a certain day. 我想写一个返回查询lastNamecustomers表, bidAmountbidTimebids表格和productNameproduct表中的某一天。 The bid table has both custom.customerID and product.productID as foreign keys so it can access the related data. 出价表同时具有custom.customerIDproduct.productID作为外键,因此它可以访问相关数据。 The query I have written is 我写的查询是

SELECT lastName, bidAmount, bidTime, productName 
FROM product, customer, bids 
WHERE bidTime BETWEEN '06/19/2014 12:00:01 AM' AND '06/19/2014 11:59:59 PM';

but for some reason it is returning a copy of every bid in the time frame for every customer in the database, rather than just the customer who places the bid. 但由于某种原因,它正在为数据库中的每个客户(而不只是下标的客户)在时间范围内返回每个报价的副本。

I hope this is all making sense, I can explain in more detail if need be. 我希望这一切都是有道理的,如有需要,我可以详细解释。

the tables in question are customer bids product 有问题的表格是customer bids product

bids has customer.customerID and product.productID as foreign keys product has customer.customerID as a foreign key bids具有customer.customerIDproduct.productID作为外键product具有customer.customerID作为外键

so as far as I know it should all connect appropriately. 据我所知,它们应该都正确连接。 or am I overlooking something. 还是我忽略了某些东西。

As mentioned in the comments, you need to explicitly join the tables together - at the moment, your query is implicitly Cartesian-joining the tables. 如评论中所述,您需要将表显式地连接在一起-目前,您的查询是隐式地笛卡尔式联接表。 Try something like: 尝试类似:

SELECT c.lastName, b.bidAmount, b.bidTime, p.productName 
FROM bids b
join product p on b.product_id = p.product_id
join customer c on b.customer_id = c.customer_id
WHERE bidTime BETWEEN '06/19/2014 12:00:01 AM' AND '06/19/2014 11:59:59 PM'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM