Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Seems you want a French description if it exists, otherwise fallback to English.</p> <pre><code>SELECT item.id, COALESCE( ( SELECT lang.data FROM language l WHERE l.item = i.id AND l.language = 'fr' ), ( SELECT lang.data FROM language l WHERE l.item = i.id AND l.language = 'en' ) ) AS description FROM item i </code></pre> <p>, or this:</p> <pre><code>SELECT item.id, COALESCE(lfr.data, len.data) FROM item i LEFT JOIN language lfr ON lfr.item = i.id AND lfr.language = 'fr' LEFT JOIN language len ON len.item = i.id AND len.language = 'en' </code></pre> <p>The first query is more efficient if the probability of finding French description is high (it will not evaluate the second subquery if the first one succeeds).</p> <p>In <code>SQL Server</code>, <code>Oracle</code> and <code>PostgreSQL</code> this one will probably more efficient if you have lots of French descriptions:</p> <pre><code>SELECT item.id, COALESCE( lfr.data, ( SELECT lang.data FROM language l WHERE l.item = i.id AND l.language = 'en' ) ) AS description FROM item i LEFT JOIN language lfr ON lfr.item = i.id AND lfr.language = 'fr' </code></pre> <p>This query will use an efficient method (<code>HASH JOIN</code> or <code>MERGE JOIN</code>) to join the French descriptions, and will fallback to English one only if necessary.</p> <p>For <code>MySQL</code>, the <code>1st</code> and the <code>3rd</code> queries make no difference.</p> <p>In all systems, create a composite index on <code>language (item, language)</code></p>
    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.
    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