![]() |
SELECT DISTINCT returning duplicates
Hello. I have a table title_views with each row being a "view" with a user_id, title_id, prog_id, date, and id. I want to gather just the user_ids without it returning duplicate user_ids. Right now I'm receiving duplicate user_id. Any help is appreciated. Thank you.
PHP Code:
|
This isn't caused by your query. This is caused by your looping and many queries.
Since you execute this query and it only retrieves one record: PHP Code:
If the tables were properly normalized, a query could be written that simply joins as necessary. Without it, you need to capture $row['user_id'] in an array, and write a array_unique call to remove duplicates after the foreach (possibly the while, depending on at what level the userid is considered unique - programs or title views). You can also change the where clause to use an IN and provide it with all the title_id's required. |
thank you for the reply. how can i write this without having the queries in the loop to accomplish what i need? I need that title_id to run the query which is in array so I used a loop... I'm not sure how I can accomplish this
|
I give up. If all you want are *ALL* the user_ids, once each, why are you involving the programs table, at all?
And why are you mucking with WHERE title_id = '$trimmed' and prog_id = '$prog_id' ") at all?To get what you *SAY* you want, you simply need to do Code:
SELECT DISTINCT user_id FROM title_views |
| All times are GMT +1. The time now is 09:30 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.