RON_ron Posted September 14, 2010 Share Posted September 14, 2010 How do I pull data by ignoring duplicate records? E.g. I've got 100 types of books and a field in mysql saying 'category'. In the books I've got 20 books as history, 5 books as agriculture, etc... I need the data displayed only the types of categories. history agriculture etc... $SomeVar = $_POST['finco']; $query = "SELECT * FROM abcs WHERE typesz = '".$SomeVar."' ORDER BY typesz ASC"; $results = mysql_query($query); $returnS=""; while($line = mysql_fetch_array($results)) { Quote Link to comment https://forums.phpfreaks.com/topic/213364-ignore-duplicate-records/ Share on other sites More sharing options...
kickstart Posted September 14, 2010 Share Posted September 14, 2010 Hi Select just the category and use DISTINCT. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213364-ignore-duplicate-records/#findComment-1110884 Share on other sites More sharing options...
RON_ron Posted September 14, 2010 Author Share Posted September 14, 2010 Thanks. One more thing, How can I add a condition to it. E.g. Retun the data using DISTINCT only when "YES" is found in field AVAILABILITY? Quote Link to comment https://forums.phpfreaks.com/topic/213364-ignore-duplicate-records/#findComment-1110888 Share on other sites More sharing options...
kickstart Posted September 14, 2010 Share Posted September 14, 2010 Hi DISTINCT returns unique rows. So just put a where cause to exclude unwanted rows first. SELECT DISTINCT category FROM bookTable WHERE availability = 'YES' Note that if you return other columns of data the code will return distinct combinations. So you could have 2 categories of agriculture returned if the other field was different. Logically if you still want only a single occurrence of each category to be returned then you need to specify which one is returned. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213364-ignore-duplicate-records/#findComment-1110910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.