tags:

views:

21

answers:

1

Hello,

I have a mysql query that shows the users last viewed documents. Some sort of history view, and I want to show this in a history tab, but I cannot get this to work.

The history is saved on another server so I'm getting a list of ID's, and I have to get the document's info from the document database. I want it to show duplicate entries in the history (Which is currently not working)

I've tried multiple things to get it to work, and all I got is get the last 25 documents which are selected with a DISTINCT on the id field, but I also want to get duplicates.

This is the query I'm using (Filled with example data)

SELECT 
    stuff_from_table
FROM 
    a_secret_table
WHERE 
    `id`
IN ( 6176, 6176, 6176, 3091, 3454, 5927, 5929, 5938, 1975, 177, 6123, 5943, 4325, 4326, 4327, 6176, 4327, 1990, 3400, 2650, 2649, 2649, 6176, 4297, 6145 ) 
ORDER BY FIND_IN_SET( id, '6176, 6176, 6176, 3091, 3454, 5927, 5929, 5938, 1975, 177, 6123, 5943, 4325, 4326, 4327, 6176, 4327, 1990, 3400, 2650, 2649, 2649, 6176, 4297, 6145') 
LIMIT 0 , 25

This code works, but will not return any duplicates. It will only return like 17 results, because it ignores the duplicates.

Anyone knows how to get this query to also return duplicates? Or do I have to manually add the missing ones in php?

Thanks in advance!

A: 

Rather than use an IN clause, you need to join on the id values. IE:

  SELECT t.stuff
    FROM YOUR_TABLE t
    JOIN (SELECT 6176 AS tid,
          UNION ALL
          SELECT 6176
          UNION ALL
          SELECT 6176
          UNION ALL
          SELECT 3091) x ON x.tid = t.id
ORDER BY FIND_IN_SET(t.id, '6176, 6176, 6176, 3091') 
   LIMIT 25

You need to split that comma separated list into a table with a single column - see: http://forge.mysql.com/tools/tool.php?id=4

OMG Ponies
It sounds counter-intuitive, but the join ensures that rows that match are returned in the resultset. Usually people want the reverse - they want to get rid of duplicates, and need to use `IN` or `EXISTS` to do so rather than using `GROUP BY` or `DISTINCT`...
OMG Ponies
Thanks man, you got me going with the union and the possibility to create a temporarily table. The code I am using now is[code] $documents = explode(",",$data);$queryData = "";for($i = 0; $i < count($documents); $i++){ $queryData .= "UNION ALL SELECT {$documents[$i]} ";}$query = "SELECT t.title as title, t.creator as creator, t.id as id, t.creationDate as createionDate, t.lastEdit as lastEdit, t.lastEditedBy as lastEditedByFROM document_data tJOIN ( SELECT 0 AS id $queryData)x ON x.id = t.id";[/code]
Jorik
@Jorik: Cool, because you have duplicates you need to use `UNION ALL` because `UNION` would remove the duplicates.
OMG Ponies
@OMG Ponies: Yea I read that on other question indeed, thanks again! Actually learned a few neat MySQL tricks too. I clicked the check image under your answer, does that automatically set the question to answered? I'm kinda new here, so I don't really know how it all works here
Jorik
@Jorik: Yep, just need to click the checkmark under the number on the left to mark an answer for the question. You won't be able to upvote (by clicking the arrow on the top of the number) until you have 15 reputation; can't downvote until 100.
OMG Ponies