Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL performance between multi join queries vs. using function
    primarykey
    data
    text
    <p>I have a table which is named: <code>candidate_info</code> has three columns are <code>id_user</code>, <code>id_type</code> and <code>value</code>:</p> <ul> <li>id_type = 1 => Fullname</li> <li>id_type = 2 => Email</li> <li>id_type = 3 => Mailing Address</li> <li>id_type = 4 => Phone number</li> </ul> <p>to store user information:</p> <p>I consider two queries as below, it will combine from rows to columns:</p> <p>Form Original:</p> <pre><code>Id User | Id Type | Value 1 | 1 | John 1 | 2 | john@g.com 1 | 3 | Ho Chi Minh 1 | 4 | 123 2 | 1 | Ana 2 | 2 | ana@g.com 2 | 3 | New York 2 | 4 | 456 </code></pre> <p>To New:</p> <pre><code>Id User | Fullname | Email | Mailing_Address | Phone Number 1 | John | john@g.com | Ho Chi Minh | 123 2 | Ana | ana@g.com | New York | 456 </code></pre> <p><strong>1. First query:</strong></p> <pre><code>select c1.id_user, c1.value as fullname, c2.value as email, c3.value as mailing_address, c4.value as phone_number from candidate_info c1 left join candidate_info c2 ON c1.id_user = c2.id_user left join candidate_info c3 ON c1.id_user = c3.id_user left join candidate_info c4 ON c1.id_user = c4.id_user where c1.id_type = 1 and c2.id_type = 2 and c3.id_type = 3 and c4.id_type = 4; </code></pre> <p><strong>2. Second query</strong></p> <pre><code>select c.id_user, MAX(IF(c.id_type = 1, c.value, NULL)) as fullname, MAX(IF(c.id_type = 2, c.value, NULL)) as email, MAX(IF(c.id_type = 3, c.value, NULL)) as mailing_address, MAX(IF(c.id_type = 4, c.value, NULL)) as phone_number from candidate_info c where c.id_type in (1, 2, 3, 4) group by c.id_user </code></pre> <p>Which is better?</p> <p>Edit: Change <code>id_entry_field</code> to <code>id_type</code>, <code>join</code> to <code>left join</code> to make sense.</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. 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