如何在MySQL中选择具有特定条件的行?

时间:2014-05-19 10:10:20

标签: mysql sql

我在mysql中有一个包含两列的表。身份证号码。首次按

列出4到13之间的所有行

SELECT * FROM table WHERE id BETWEEN 4 AND 13

现在我有一个从4到13的所有行的列表。(绿色行)

我想在此列表中搜索并找到number为< = 10的第一行以及该数字为< = 1000的最后一行以及其间的所有行。即我需要下图所示的橙色行:

enter image description here

所以,我需要这样的东西:

SELECT *
FROM TABLE
WHERE id BETWEEN 4 AND 13
  AND number
  START
  FROM <= 10
  AND END WITH <= 1000

但显然上述情况并不正确。我怎样才能获得橙色行?

6 个答案:

答案 0 :(得分:6)

这应该适合你:

SELECT  T.ID, T.Number
FROM    T
        INNER JOIN
        (   SELECT  MIN(CASE WHEN Number < 10 THEN ID END) AS FirstID,
                    MAX(CASE WHEN Number < 1000 THEN ID END) AS LastID
            FROM    T
            WHERE   ID BETWEEN 4 AND 13 
        ) AS ID
            ON ID.FirstID <= T.ID
            AND ID.LastID >= T.ID

关键是子查询 -

SELECT  MIN(CASE WHEN Number < 10 THEN ID END) AS FirstID,
        MAX(CASE WHEN Number < 1000 THEN ID END) AS LastID
FROM    T
WHERE   ID BETWEEN 4 AND 13 

在给定范围内获得小于10的第一个ID,并且在给定范围内获得小于1000的最后一个ID。然后使用这些ID来过滤结果。

<强> Example on SQL Fiddle

答案 1 :(得分:1)

这样的事情:

SELECT MyTable.* FROM 
  (SELECT * FROM T WHERE id BETWEEN 4 AND 13) as MyTable
JOIN (
  SELECT ID, NUMBER 
  FROM T 
  WHERE id BETWEEN 4 AND 13 
  AND NUMBER <= 10 LIMIT 1
  ) as StartRow
JOIN (
  SELECT ID, NUMBER 
  FROM T       
  WHERE id BETWEEN 4 AND 13 
  AND NUMBER <= 1000 
  ORDER BY ID DESC 
  LIMIT 1
  ) as EndingRow
WHERE MyTable.ID BETWEEN StartRow.ID and EndingRow.ID

请参阅sqlFiddle Demo working here

您选择子查询,绿色行代表ID 4到13。 然后在主选择中,使用joins获取橙色行的边框。

让我们一步一步走。 首先,在id 4和13(绿行)之间选择行,并将结果命名为MyTable。

SELECT * FROM 
      (SELECT * FROM T WHERE id BETWEEN 4 AND 13) as MyTable

现在您需要知道此子集中第一个低于或等于10(<=10)的元素。 在我的方法i join中再次给出结果并将此子句添加到它中,同时使用Limit将结果集缩小为1。

JOIN (
      SELECT ID, NUMBER 
      FROM T 
      WHERE id BETWEEN 4 AND 13 
      AND NUMBER <= 10 LIMIT 1
      ) as StartRow

现在,对于4 to 13结果中的每一行,您都有字段StartRow.ID。 同样适用于橙色列表的末尾。 4 to 13的最后一个是&lt; = 1000。

JOIN (
      SELECT ID, NUMBER 
      FROM T       
      WHERE id BETWEEN 4 AND 13 
      AND NUMBER <= 1000 
      ORDER BY ID DESC 
      LIMIT 1
      ) as EndingRow

通过将DESC添加到ORDER语句,我得到的最后一个元素与我的查询匹配,而不是第一个。

现在我们只需将其放入封装Query的Where子句:

  WHERE MyTable.ID BETWEEN StartRow.ID and EndingRow.ID

答案 2 :(得分:0)

试试这个

$arr = array();
$res = mysql_query("SELECT * FROM table WHERE id BETWEEN 4 AND 13 ") or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
    $arr[$row['id']] = $row['number']; // put id and number in array
}

$first_key = 0; 
$last_key = 0; 


// getting first key    
foreach($arr as $key=>$val)
{
    if($val <= 10)
    {
        $first_key = $key; 
        break;
    }
}

// getting last key 
$arr_reverse = array_reverse($arr);
foreach($arr as $key=>$val)
{
    if($val <= 1000)
    {
        $last_key = $key; 
        break;
    }
}

// getting values form first key to last key
$arr_output = array();

foreach($arr as $key=>$val)
{
    if($key>=$first_key && $key<=$last_key)
    {
        $arr_output[$key] = $val;
    }
}

print_r($output);

注意:mysql_ *已弃用。我用这里的例子。使用mysqli或pdo

答案 3 :(得分:0)

请尝试此查询:

SELECT * FROM table
WHERE ID BETWEEN
(SELECT MIN(id)FROM
(
 SELECT *
 FROM table
 WHERE id BETWEEN 4 AND 13
) s 
WHERE number <= 10)
AND
(SELECT MAX(id)FROM
(
 SELECT *
 FROM table
 WHERE id BETWEEN 4 AND 13
) s 
WHERE number <= 1000)

答案 4 :(得分:-1)

怎么样......

SELECT * FROM table WHERE id BETWEEN 4 AND 13 AND number BETWEEN 10 AND 1000

答案 5 :(得分:-2)

尝试:

SELECT * FROM (SELECT * FROM TABLE WHERE (id BETWEEN 4 AND 13)) WHERE NUMBER BETWEEN 10 AND 1000