【发布时间】:2016-01-03 05:10:42
【问题描述】:
我想就mysql查询寻求这个论坛的专业建议,我花了几个小时试图让它正确但无济于事。
所以事情是这样的。 Query1 下面我将从 5 个表中检索名称、雇主 ID、日期、班次 ID、班次标签、班次时间、商店 ID、商店名称、角色 ID 和角色标签,tblshift、tblstore、@987654327 @、tblschedule 和 employee。
表 tblshift、tblstore、tblrole 和雇主通过它们各自的表中的 id 链接到 tblschedule。 php 变量 empstore 和 empdate 是从表单发布的值。
$query1 = '选择 a.name, a.empid, b.keydate, c.shiftid, c.shiftlabel, c.shifttime, c.shifttime2, d.storeid, d.storelabel, e.roleid , e.rolelabel from employee as a, tblschedule as b, tblshift as c, tblstore as d, tblrole as e where a.empid=b.empid and b.shiftid=c.shiftid and b.storeid=d.storeid and b .roleid=e.roleid 和 d.storeid='.$empstore.'和 b.keydate ="' . $empdate . '"';
上述查询工作正常,但我还想检索与此查询相反的查询,即查询中不存在的行。 我尝试在查询中使用“NOT EXIST”和“NOT IN”语句,但查询无法运行或行不正确。请参阅下面使用“NOT IN”和“NOT EXISTS”语句的查询。从 查询 2 和 3,您会发现我使用了 empid、keydate 和 shiftid,因为它们是我的 tblschedule 表中的主键,而其他列都是基于这种唯一性。
*$query2 = 'select a1.name, a1.empid, b1.keydate, c1.shiftid, c1.shiftlabel, c1.shifttime, c1.shifttime2, d1.storeid, d1.storelabel, e1.roleid, e1.rolelabel from employee as a1, tblschedule as b1, tblshift as c1, tblstore as d1, tblrole as e1 where a1.empid=b1.empid and b1.shiftid=c1.shiftid and b1.storeid=d1.storeid and b1.roleid=e1.roleid'
. ' where (a1.empid, b1.keydate, c1.shiftid) not in (select a2.empid, b2.keydate, c2.shiftid from employee as a2, tblschedule as b2, tblshift as c2, tblstore as d2, tblrole as e2 where a2.empid=b2.empid and b2.shiftid=c2.shiftid and b2.storeid=d2.storeid and b2.roleid=e2.roleid and d2.storeid='.$empstore.' and b2.keydate ="' . $empdate . '")';
$query3 = 'select a1.name, a1.empid, b1.keydate, c1.shiftid, c1.shiftlabel, c1.shifttime, c1.shifttime2, d1.storeid, d1.storelabel, e1.roleid, e1.rolelabel from employee as a1, tblschedule as b1, tblshift as c1, tblstore as d1, tblrole as e1 where a1.empid=b1.empid and b1.shiftid=c1.shiftid and b1.storeid=d1.storeid and b1.roleid=e1.roleid'
. ' where not exists (select 1 from employee as a2, tblschedule as b2, tblshift as c2, tblstore as d2, tblrole as e2 where a2.empid=b2.empid and b2.shiftid=c2.shiftid and b2.storeid=d2.storeid and b2.roleid=e2.roleid and d2.storeid='.$empstore.' and b2.keydate ="' . $empdate . '" and a1.empid=a2.empid and b1.keydate=b2.keydate and c1.shiftid=c2.shiftid)';*
关于查询的复杂部分是我试图从其他表的多个列中收集数据,并且我还根据唯一列 empid、keydate 和 shiftid 过滤数据。我在下面的这个网站上找到了一些来源,但无法正常工作。
How to retrieve non-matching results in mysql
get the opposite results from a SELECT query
mysql "Where not in" using two columns
提前致谢,期待在这里向大家学习。
问候, 丹尼斯
【问题讨论】:
-
查看连接。左和内连接