SQL union: Did you remember to make the first column something unique in your call to get_records?

Re: SQL union: Did you remember to make the first column something unique in your call to get_records?

by Diane Soini -
Number of replies: 4

Thank you. get_recordset_sql is what I needed.

Oh wait, no it is not the answer because I get a different result from a direct SQL query in the database. It doesn't throw out the duplicates.

In reply to Diane Soini

Re: SQL union: Did you remember to make the first column something unique in your call to get_records?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers

in your queries you can add

RAND() unid

Give something like

SELECT RAND() mainid, t1.* FROM (

....

Hope it's help.

In reply to Dominique Palumbo

Re: SQL union: Did you remember to make the first column something unique in your call to get_records?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers

postgress doesnt support rand() but random()

The other way more sql standard I found it's use concat.


In my case for ex :

select  concat(t1.userid , t1.groupid) unid ...

In reply to Dominique Palumbo

Re: SQL union: Did you remember to make the first column something unique in your call to get_records?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you really want that to be cross-db, use Moodle's $DB->sql_concat('t1.userid', 't1.groupid') function.

In reply to Tim Hunt

Re: SQL union: Did you remember to make the first column something unique in your call to get_records?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers

Thank you. Probably basic for you but really good info for me Yes