简体   繁体   中英

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. The bid table has both custom.customerID and product.productID as foreign keys so it can access the related data. 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

bids has customer.customerID and product.productID as foreign keys product has customer.customerID as a foreign key

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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