SQL算法从一个表三个标识符 [英] SQL algorithm with three identifiers from one table

查看:275
本文介绍了SQL算法从一个表三个标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这将是很容易的,但我想不通,怎么获取必要的值从我的数据库中有一个查询。只是不出来了。我打算让codeIginiter系统内部此查询。

表信息建设:

  CREATE TABLE信息(
    planid INT(11)NOT NULL,
    production_nr INT(11)NOT NULL,
    状态INT(11)NOT NULL
);
 

表信息的内容:


必要的输出: 我想获得(在最好的 - 只有一个查询,但如果它不是可能的,那么多)的所有planid的在那里。所有这些计划ID的pruduction_nrs的状态为> = 3

在这种情况下,我需要得到这些plandid的:2和5,因为每种planid的所有production_nrs的状态为大于或等于3

解决方案

 选择planid,production_nr
从信息INF1
其中,不存在(从信息INF2选择1
                  其中,inf1.planid = inf2.planid
                  和状态< 3)
 

您可能会考虑修改SELECT子句根据您的需要(第一行):

  • 添加不同的(如表PK包括状态栏)

  • 更​​改列清单

Probably this will be really easy, but I can't figure out, how to get necessary values from my DB with one query. Just can't figure it out now. I'm going to make this query inside CodeIginiter system.

Table 'information' construction:

CREATE TABLE information (
    planid int(11) NOT NULL,
    production_nr int(11) NOT NULL,
    status int(11) NOT NULL
);

Table 'information' content:


Necessary output: I would like to get (at the best - with only one query, but if its not possible, then with multiple) all planid's where: ALL of this plan id's pruduction_nrs has status >= 3.

In this case, I would need to get these plandid's: 2 and 5 because each of these planid's ALL production_nrs has status greater or equal than 3.

解决方案

select planid, production_nr
from information inf1
where not exists (select 1 from information inf2
                  where inf1.planid = inf2.planid
                  and   status < 3)

You might consider amending the select clause (first row) according to your needs:

  • Add distinct (if the table PK includes status column)

  • Change the column list

这篇关于SQL算法从一个表三个标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆