Note that there are some explanatory texts on larger screens.

plurals
  1. POJoin Nearest Date
    primarykey
    data
    text
    <p>I have read through a ton of responses on here but nothing is working quite as well as I would like. I currently have a working query that includes 2 sub queries, the problem is that it takes about 10 seconds to execute. I was wondering if there is any way to make this go quicker, maybe with a join. I just can't seem to get my head out of the box it is in. Please let me know your thoughts.</p> <p>Here is the working query:</p> <pre><code>Select concat(a.emp_firstname, ' ', a.emp_lastname) as names , if(if (a.emp_gender = 1, 'Male', a.emp_gender)=2, 'Female', if (a.emp_gender = 1, 'Male', a.emp_gender)) as emp_gender , c.name , a.emp_work_telephone , a.emp_hm_telephone, a.emp_work_email , a.custom7, a.employee_id , a.city_code, a.provin_code, d.name as status, (SELECT cast(concat(DATE_FORMAT(e.app_datetime, '%H:%i'), ' ', e.app_facility) as char(100)) FROM li_appointments.li_appointments as e where e.terp_id = a.employee_id and e.app_datetime &lt;= str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p') and date(e.app_datetime) = date(str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p')) order by e.app_datetime desc limit 1) as prevapp, (SELECT cast(concat(DATE_FORMAT(e.app_datetime, '%H:%i'), ' ', e.app_facility) as char(100)) FROM li_appointments.li_appointments as e where e.terp_id = a.employee_id and e.app_datetime &gt; str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p') and date(e.app_datetime) = date(str_to_date('06/26/13 at 3:20 PM', '%m/%d/%Y at %h:%i %p')) order by e.app_datetime desc limit 1) as nextapp from hs_hr_employee as a Join hs_hr_emp_skill as b on a.emp_number = b.emp_number Join ohrm_skill as c on b.skill_id = c.id Join orangehrm_li.ohrm_employment_status as d on a.emp_status = d.id where c.name like '%Arabic%' and d.name = 'Active' order by rand(); </code></pre> <p><code>EXPLAIN</code> results:</p> <pre> +----+--------------------+-------+--------+---------------------+------------+---------+---------------------------+-------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+--------+---------------------+------------+---------+---------------------------+-------+----------------------------------------------+ | 1 | PRIMARY | d | ALL | PRIMARY | | | | 10 | Using where; Using temporary; Using filesort | | 1 | PRIMARY | a | ref | PRIMARY,emp_status | emp_status | 5 | orangehrm_li.d.id | 48 | Using where | | 1 | PRIMARY | b | ref | emp_number,skill_id | emp_number | 4 | orangehrm_li.a.emp_number | 1 | | | 1 | PRIMARY | c | eq_ref | PRIMARY | PRIMARY | 4 | orangehrm_li.b.skill_id | 1 | Using where | | 3 | DEPENDENT SUBQUERY | e | ALL | | | | | 28165 | Using where; Using filesort | | 2 | DEPENDENT SUBQUERY | e | ALL | | | | | 28165 | Using where; Using filesort | +----+--------------------+-------+--------+---------------------+------------+---------+---------------------------+-------+----------------------------------------------+ </pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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