Fetch duplicate values in a query

Fetch duplicate values in a query

by Dimitrios Desyllas -
Number of replies: 1

Greetings I made a following table named block_userlist containing login and logout statistics:

+------------------+------------+------+-----+---------+----------------+
| Field            | Type       | Null | Key | Default | Extra          |
+------------------+------------+------+-----+---------+----------------+
| id               | bigint(10) | NO   | PRI | NULL    | auto_increment |
| user_id          | bigint(10) | NO   |     | NULL    |                |
| logout_time      | bigint(10) | YES  |     | NULL    |                |
| login_time       | bigint(10) | NO   |     | NULL    |                |
| session_duration | bigint(10) | YES  |     | NULL    |                |
+------------------+------------+------+-----+---------+----------------+


And on my rdbms when I try to fetch it like that:


SELECT user.username,login_time,logout_time,FROM_UNIXTIME(session_duration) FROM user JOIN block_userlist on user.id=block_userlist.user_id ORDER BY login_time DESC;
The results are fetched correctly but when I try it via get_records_sql I want to fetch the data even if I get duplicate values on username field. Because on my moodle custom block I get the following error:

Did you remember to make the first column something unique in your call to get_records? Duplicate value 'admin' found in column 'username'.

So I want to know how I can fetch duplicate values from my database in queries like thar? In my block's logic is acceptable to fetch duplicate values.
Average of ratings: -
In reply to Dimitrios Desyllas

Re: Fetch duplicate values in a query

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Try fetching block_userlist.id as the first field returned, that will make every returned record have a unique first field. I also note you've not put { } around the table names, which you need to do for Moodle to automatically insert the table prefix ("mdl_" by default on new installs).

Alternatively you could use get_recordset_sql - which doesn't create an indexed array, so doesn't need a unique first field.