I have the tables PRODUCTS and LISTINGS. When doing the following query:
SELECT DISTINCT *
FROM products
INNER JOIN listings
ON products.product_number=listings.product_number
This is the “search” functionality:
WHERE products.product_number !=''
AND listings.monthly_price BETWEEN '0' AND '10'
This returns a double entry of one of the product listings. Why isn’t DISTINCT working?
EDIT
Products:
product_number, make, model model_number, colour, processor, battery_standby, battery_talk, camera, flash, screen_size, screen_res, memory
Listings:
listing_number, featured, date, member_id, network, length, product_number, monthly_price, minutes, texts, data, image1
Essentially I’d like to create result rows matching the listings tables via their PRODUCT_NUMBER to the product table. It’s for a search function of a phone listings website to be more precise.
To be much more specific, the search function uses the products table to search, then the listings table to show the useful information about the phone listing.
ANSWER
SELECT DISTINCT *
FROM listings
INNER JOIN products
ON products.product_number=listings.product_number
The above did the trick; simply swapping the tables round. I also inserted a few more rows into listings, and the “problem” vanished. Even if it’s not solved, it isn’t happening anymore… Not sure what the problem was.
If there is a record where 2 listings joins a single product then this would produce what you are seeing:
The select distinct is done on the result of the inner join
I’d use the common join value to select * from each table and see the results
HTH
Ian