mysql:select语句给出错误

时间:2016-03-13 04:46:41

标签: mysql

我知道我使用了错误的标题,因为我无法为我的问题考虑一个合适的术语。我正在做一个文件跟踪系统。

问题在于:在下一个处于较高职位的人员之前,需要先由下层人员签署文件。

为了做到这一点,我提出了在我的桌子上使用水平的想法。

但我缺乏MySQL知识。这是我的MySQL表:

enter image description here

这是我的MySQL查询。问题是,我不想表现出来 LHPL003文件尚未因为其他签名级别低于他尚未签署的文件(如果签字人签署了该文件,则列状态将变为0)。

我该怎么做?

SELECT `tracknum`, `signatoryid`, `signatorylevel`, `status`
from tble_transaction 
where signatoryid = "LHPL003" and signed = 'Released' and signatorylevel <= "3";

问题2:

我的第一个问题有问题

现在我有了这张桌子

enter image description here

我该怎么办?例如,级别为1的用户已经对文档进行了签名,以便它成为 LHPL005状态= 0

我想要的是只有LHPL004才能看到文档的查询

LHPL003和LHPL002首先看不到该文件,因为那些杠杆低于他们的人还没有签署该文件

3 个答案:

答案 0 :(得分:1)

SELECT `tracknum`, `signatoryid`, `signatorylevel`, `status`
from tble_transaction 
where signatoryid != "LHPL003" and signed = 'Released' and status='0' and signatorylevel <= "3";

答案 1 :(得分:1)

显示状态为0且具有最小签名级别(来自用户的输入)的行

 SELECT tracknum, signatoryid,signatorylevel, status from track where status=0  and signatorylevel < 4 order by signatorylevel limit 1;

修改:
如果您希望多个记录使用以下查询

,则上述查询将仅生成1条记录
select * from track where signatorylevel = (select min(signatorylevel) from track where status = 0 and signatorylevel < 4 );

答案 2 :(得分:1)

我不明白你想要什么,但我认为这两个查询之一有适合你的解决方案:

SELECT * FROM `tble_transaction` as t1
WHERE signatoryid = 'LHPL003' and signed = 'Released'
AND EXISTS (select 1 from tble_transaction where signatorylevel < t1.signatorylevel and status = 0)

第二个:

Select * from tble_transaction where
 signatorylevel < (SELECT min(signatory level) FROM `tble_transaction` as t1 WHERE signatoryid = 'LHPL003' and signed = 'Released' and status = 1)
ORDER BY signatory level desc
Limit 1

第二个问题:

SELECT * FROM `tble_transaction` as t1
WHERE signatoryid = 'LHPL005' and signed = 'Released'
AND NOT EXISTS (select 1 from tble_transaction where signatorylevel < t1.signatorylevel and status = 1)
-- ORDER BY signatorylevel LIMIT 1 -- you can ignore this if signatoryid is UNIQUE