SQL Query连接两个表并从第一个过滤行而没有相应的第二个条目

时间:2015-12-17 19:28:15

标签: mysql sql join

我在Mysql数据库中有2个表: 一个是员工表来存储员工详细信息:员工

emp_id name
1       ABC   
2       XYZ   
3       EFG   

其次是月工资:工资

 wage_id  emp_id month year   wage   paid
 1        1      11    2015   2000   2000
 2        2      11    2015   1000   1000
 3        3      11    2015   1500   1700
 4        1      12    2015   2000   2000
 5        3      12    2015   1500   0

我必须过滤那些月份没有输入工资或者工资低于或高于工资的员工。对于前。

月份= 11时

Name  Wage Paid
EFG   1500 1700

当月= 12时

Name  Wage  Paid
XYZ   0     0
EFG   1500  0

我尝试了几件事,但无法获得所需的输出。

Select  employee.emp_id, 
        employee.name, 
        wages.emp_id, 
        wages.wage_id, 
        wages.wage, 
        wages.paid 
From    employee 
Join    wages       On  employee.emp_id = wages.emp_id 
Where   wages.month = 11 
And     wages.year = 2015 
And     wages.wage <> wages.paid

Select  wages.wage, 
        wages.paid, 
        wages.emp_id, 
        employee.emp_id 
From    wages 
Join    employee    On  wages.emp_id = employee.emp_id 
Where   wages.month = 11 
And     wages.year = 2015 
And     wages.wage <> wages.paid

请建议更改。

3 个答案:

答案 0 :(得分:1)

这应该有效:只需将[月份]更改为您想要的任何内容。

node ./bin/www

Error: Cannot find module './lib/querystring'
    at Function.Module._resolveFilename (module.js:327:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:355:17)
    at require (internal/module.js:13:17)
    at Object.<anonymous> (/Users/dfranc3373/Project/node_modules/request/request.js:22:19)
    at Module._compile (module.js:399:26)
    at Object.Module._extensions..js (module.js:406:10)
    at Module.load (module.js:345:32)
    at Function.Module._load (module.js:302:12)
    at Module.require (module.js:355:17)
    at require (internal/module.js:13:17)

答案 1 :(得分:0)

  1. 在月份,年份和emp_id上进行外部联接以获取工资表中缺少的行: select e.name, ifnull(w.wage,0) as wage, ifnull(w.paid,0) as paid from employee e left join wage w on w.year=2015 and w.month=11 and w.emp_id=e.emp_id
  2. 获取工资为空或不等额的行:where w.wage is null or w.wage<>w.paid

答案 2 :(得分:0)

<html lang="en">
<head>
    <script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script>
    <script src="/Scripts/Helpers.js"></script>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Title Goes Here</title>
</head>

<body>
    <form>
        Note:<br>
    </form>
    <textarea rows="4" cols="50" name="note" form="form">

    </textarea>
    <br/>
    <input type="date" name="day" form="form">
    <input type="submit" id="create" value="Submit" form="form">
</body>
</html>

试试这个