Forum Moderators: coopster

Message Too Old, No Replies

trying to avoid duplicate content with a query

php, code, query,

         

hanyaz

4:42 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Hello,
I have some articles stored in databse, the db is updated automatically. However it happens that some data are stored twice.
Thay have the same title but an different url.
Can i customise my query to avoid seeing duplicate articles listed on my page ?
Something pulling only one item, and ignoring any other article having the same title.
Thanks in advance for any help
Yaz

Anyango

4:53 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




If your table was named tableName and title field was named pageTitle,

try this.

SELECT DISTINCT pageTitle from tableName

that will not select 2 articles having the same title and hence all your titles will be unique

henry0

4:55 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<edit>I did not shoot fast enough</edit>

If you select a single one among a few
how do you decide on which url to use? -does it matter?-
Without seeing the query (you may post it, just make sure to replace the sensible info by any char to your like) you might try something in the line of
SELECT DISTINCT

Anyango

4:59 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:P

hanyaz

8:03 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



thank u guys...i always get answers from this forum
Yaz

hanyaz

9:31 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Hello again...
I have tried your suggestion, it works :
$query1 = "SELECT DISTINCT ItemTitle FROM feedItems ORDER BY ItemAddedTime DESC";

However if i want to use other table fields, necessary for master detail table. What can i do ?

i have tried the following :
$query1 = "SELECT DISTINCT ItemTitle, itemID FROM feedItems ORDER BY ItemAddedTime DESC";

It displays the duplicated articles again...
What should i do ? A second query ?
Thanks
yaz

henry0

10:25 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you find some limitations/definitions
called a sub query

for example:
WHERE
id='$id'
AND
url='$url'

you may also instead of WHERE use OR or ¦¦
(Beware forum breaks pipes)

Anyango

3:55 am on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is a very Good question and honestly i don't know the best solution nor it is answered anywhere on the web that is easily found on search engines. Even when we search web we find many people trying to look for solution of this problem, that with the DISTINCT field they also want other fields returned too, while keeping the DISTINCT value distinct so that it is not repeated. As Henry said Sub Query is also a solution, and this can also be a solution.

( please don't mind if this query looks like greek because it appears complex, but it will work in your case right away, copy paste )

SELECT feedItems1.itemID,feedItems1.ItemTitle FROM feedItems as feedItems1
INNER JOIN feedItems as feedItems2
ON feedItems1.itemID=feedItems2.itemID
GROUP BY feedItems2.ItemTitle
ORDER BY feedItems2.ItemAddedTime DESC

it will return itemID,ItemTitle for DISTINCT ItemTitle(s) ordered by ItemAddedTime DESC

hanyaz

10:09 am on Sep 19, 2008 (gmt 0)

10+ Year Member



Thank you so much Anyango
That's solved the problem.
Yaz