简体   繁体   中英

Query select in select join table and where if condition

I have 2 table relationships.

This is the employees table:

"id"    "nik"        "namalengkap"  "tgl_join"  "resign_date"
"1" "1802012637"    "Agung"         "2013-02-18"    NULL
"2" "1801012633"    "Karyawan"      "2017-07-12"    "2018-08-06"
"3" "1804232684"    "Test Data"     "2018-08-21"    NULL

This is the employee_historypositions table:

"id"    "nik"   "tgl_awal"  "tgl_akhir" "level_id"  "jabatan_id"    "departemen_id" "divisi_id" "direktorat_id" "publish"
"1" "1802012637"    "2017-05-12"    "2017-09-12"    "1" "[""3"",""4""]" "1" "2" "1" "1"
"2" "1802012637"    "2018-06-12"    "2018-07-12"    "2" "[4,2]" "1" "9" "3" "1"
"3" "1801012633"    "2018-07-26"    \N  "2" "[3,2]" "1" "11"    "3" "1"
"4" "1801012633"    "2018-09-10"    "2018-07-21"    "6" "[4]"   "1" "3" "1" "0"
"5" "1804232684"    "2018-07-21"    "2018-08-21"    "10"    "[4]"   "1" "2" "1" "1"

I want to show data d, nik, nama lengkap, jabatan, level, departemen, divisi active in July, 2018

Using this condition: Condition in employees table:

  • join_date is less than or equals 7th month of 2018 AND
  • resign_date equals NULL or resign_date greater than 7th month of 2018

Condition in employee_historypositions table:

  • publish equals 1
  • tgl_awal is less than 7th month of 2018
  • if between there are duplicate data in the resutls, then the latest duplicated data must be shown.

How do I get the query?

Can anybody help me? Thanks

i have try this query

SELECT a.*, b.nik, b.namalengkap,b.tgl_join, b.resign_date 
FROM employee_historypositions a
LEFT OUTER
JOIN 
employees b ON a.nik = b.nik
WHERE EXTRACT(YEAR_MONTH
FROM b.tgl_join) <= CONCAT("2018","07") AND b.resign_date IS NULL OR EXTRACT(YEAR_MONTH
FROM b.resign_date) > CONCAT("2018","07")

you can check from here http://sqlfiddle.com/#!9/700c31/1

but the data show all in employee_historypositions. i just wan't show data with condition like this.

  1. publish equals 1
  2. tgl_awal is less than 7 months 2018
  3. if between the data that appears there are 2 duplicate data, then the latest data is duplicated must be show.

Here is a quick way to just get all the columns for both tables. Notice using the employee_historypositions first since it contains more unique data.

select 
    t1.*,
    t2.*
from
    employee_historypositions t1
join
    employees table t2
on
    t1.nik = t2.nik;

Then you can replace the * to get the exact columns that you want.

So if you wanted one column from the first table and one from the second, you'd do:

select 
    t1.tgl_join,
    t2.tgl_awal
from
    employee_historypositions t1
join
    employees table t2
on
    t1.nik = t2.nik;

join_date is less than the same as month 7 of 2018

`tgl_join` < '2018-07-01'

resign_date equals NULL or resign_date more than 7 months 2018

`resign_date` IS NULL OR `resign_date` > '2018-07-01'

publish equals 1

`publish` = 1

*tgl_awal is less than 7 months 2018 *

`tgl_awal` < '2018-07-01'

if between the data that appears there are 2 duplicate data, then the latest data is duplicated must be show.

SELECT DISTINCT ...

So, your query might looks like :

--replace the ... by the fields you need.
SELECT DISTINCT emp.id, emp.nik, hist.jabatan_id ...
FROM `employees` emp
JOIN `employee_historypositions` hist
    ON `emp`.`nik` = `hist`.`nik`
WHERE (`emp`.`tgl_join` < '2018-07-01'
        AND (`emp`.`resign_date` IS NULL OR `emp`.`resign_date` > '2018-07-01')
        AND `hist`.`publish` = 1
        AND `hist`.`tgl_awal` < '2018-07-01')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM