Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>select a.itemId, a.name, b.itemId otherItemId, b.name othername from item a inner join item b on a.itemId &lt; b.itemId where not exists ( select * from vote v where ((v.betterItemId = a.itemId and v.worseItemId = b.itemId) or (v.betterItemId = b.itemId and v.worseItemId = a.itemId)) and v.userId = 1234) # &lt;&lt; enter the userId here order by rand() limit 1; </code></pre> <ul> <li>First generate all the a-b combinations, but use <code>a.id&lt;b.id</code> to prevent duplicates (e.g. 1-2 and 2-1).</li> <li>Next for each combination of a-b, check that (in any permutation better-worse or worse-better) the item has not already been voted by userId (example of <code>1234</code> in query)</li> <li>order all results randomly using <code>order by rand()</code></li> <li>use <code>limit 1</code> to get only 1 row</li> <li>In the select, show id and name for both items</li> </ul> <p>To show it as two separate rows, the front end should be used for this. But to do this purely in MySQL, a trick is required. The below is untested at the time of writing.</p> <pre><code>select ItemId, name from ( select a.itemId, a.name, @b := b.itemId from item a inner join item b on a.itemId &lt; b.itemId where not exists ( select * from vote v where ((v.betterItemId = a.itemId and v.worseItemId = b.itemId) or (v.betterItemId = b.itemId and v.worseItemId = a.itemId)) and v.userId = 1234) # &lt;&lt; enter the userId here order by rand() limit 1 ) c union all select itemId, name from item d on d.itemId = @b </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload