Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@fthiella 's solution is very elegant.</p> <p>If in future you want show more than <code>user_id</code> you could use joins, and there in one line could be all data you need.</p> <p>If you want to use <code>AND</code> conditions, and the conditions are in multiple lines in your table, you can use <code>JOINS</code> example:</p> <pre><code>SELECT `w_name`.`user_id` FROM `wp_usermeta` as `w_name` JOIN `wp_usermeta` as `w_year` ON `w_name`.`user_id`=`w_year`.`user_id` AND `w_name`.`meta_key` = 'first_name' AND `w_year`.`meta_key` = 'yearofpassing' JOIN `wp_usermeta` as `w_city` ON `w_name`.`user_id`=`w_city`.user_id AND `w_city`.`meta_key` = 'u_city' JOIN `wp_usermeta` as `w_course` ON `w_name`.`user_id`=`w_course`.`user_id` AND `w_course`.`meta_key` = 'us_course' WHERE `w_name`.`meta_value` = '$us_name' AND `w_year`.meta_value = '$us_yearselect' AND `w_city`.`meta_value` = '$us_reg' AND `w_course`.`meta_value` = '$us_course' </code></pre> <p>Other thing: Recommend to use prepared statements, because <code>mysql_*</code> functions is not SQL injection save, and will be deprecated. If you want to change your code the less as possible, you can use <code>mysqli_</code> functions: <a href="http://php.net/manual/en/book.mysqli.php" rel="noreferrer">http://php.net/manual/en/book.mysqli.php</a></p> <p>Recommendation:</p> <p>Use indexes in this table. <code>user_id</code> highly recommend to be and index, and recommend to be the <code>meta_key</code> AND <code>meta_value</code> too, for faster run of query.</p> <p>The explain:</p> <p>If you use <code>AND</code> you 'connect' the conditions for one line. So if you want AND condition for multiple lines, first you must create one line from multiple lines, like this.</p> <p>Tests: Table Data:</p> <pre><code> PRIMARY INDEX int varchar(255) varchar(255) / \ | +---------+---------------+-----------+ | user_id | meta_key | meta_value| +---------+---------------+-----------+ | 1 | first_name | Kovge | +---------+---------------+-----------+ | 1 | yearofpassing | 2012 | +---------+---------------+-----------+ | 1 | u_city | GaPa | +---------+---------------+-----------+ | 1 | us_course | PHP | +---------+---------------+-----------+ </code></pre> <p>The result of Query with <code>$us_name='Kovge'</code> <code>$us_yearselect='2012'</code> <code>$us_reg='GaPa'</code>, <code>$us_course='PHP'</code>:</p> <pre><code> +---------+ | user_id | +---------+ | 1 | +---------+ </code></pre> <p>So it should works.</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.
    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