簡體   English   中英

用於在表中搜索字符串的SQL查詢?

[英]SQL query to search for string in a table?

嗨,我想在存儲在mysql數據庫中的表中搜索特定的字符串。 表格結構如下: -

+--------------------------------------------------------------------------+
|    day    | 9-10    | 10-11  | 11-12    | 12-1  | 1-2    |  2-3   |  3-4 |
|---------------------------------------------------------------------------
| Monday    | DA6210  | DA6220  | DA6010  |       | DA6020 |        |      |
| Tuesday   | DA6210  | DA6010  | DA6220  |       | DA6020 |        |      |
| Wednesday | IA6010  | DA6220  |         |       | DA6020 |        |      |
| Thursday  | IA6010  |         | DA6210  |       | GC6210 |        |      |
| Friday    | IA6010  |         | DA6010  |       | GC6210 |        |      |
| Saturday  | DA6210  |         |         |       |        |        |      |
+--------------------------------------------------------------------------+

我想要搜索當天和所有時間的字符串。 對於前者 如果當天是星期四並且代碼是DA6220,它應該返回空集,因為字符串在星期四的所有時間段都不存在。

我已經創建了一個SQL查詢但它沒有按預期工作。 請提供一種方法來做到這一點。

SQl查詢: -

SELECT * from `table` where day='Thursday' AND `9-10`='DA6220' OR `10-11`='DA6220' OR `11-12`='DA6220' 
            OR `12-1`='DA6220' OR `1-2`='DA6220' OR `2-3`='DA6220' OR `3-4`='DA6220'
SELECT * from `table` where day='Thursday' AND (`9-10`='DA6220' OR `10-11`='DA6220' OR `11-12`='DA6220' 
            OR `12-1`='DA6220' OR `1-2`='DA6220' OR `2-3`='DA6220' OR `3-4`='DA6220')

看起來你的AND首先被評估。 請嘗試以上方法

已經給出了問題的答案,但是當我查看您的數據時,我會看到一些可以讓您的生活更輕松並增加可能性的東西:您最好創建一個VIEW

首先,我稍微修改了你的列名,因為你保留了單詞。

單擊此處查看SQL Fiddle

介紹你的新朋友myview

cREATE VIEW myview AS 
SELECT wkday, '9-10'  AS rng, `9-10`  AS cd from `mytable` 
UNION  
SELECT wkday, '10-11' AS rng, `10-11` AS cd from `mytable` 
UNION 
SELECT wkday, '11-12' AS rng, `11-12` AS cd from `mytable` 
UNION 
SELECT wkday, '12-1'  AS rng, `12-1`  AS cd from `mytable` 
UNION 
SELECT wkday, '1-2'   AS rng, `1-2`   AS cd from `mytable` 
UNION 
SELECT wkday, '2-3'   AS rng, `2-3`   AS cd from `mytable` 
UNION 
SELECT wkday, '3-4'   AS rng, `3-4`   AS cd from `mytable`  
;

這是針對您的數據庫的一次性查詢。

您的查看數據是:

SELECT * FROM myview

wkday           rng     cd
----------------------
Monday      9-10    DA6210
Tuesday     9-10    DA6210
Wednesday   9-10    IA6010
Thursday    9-10    IA6010
Friday      9-10    IA6010
Saturday    9-10    DA6210
Monday      10-11   DA6220
Tuesday     10-11   DA6010
Wednesday   10-11   DA6220
...

創建視圖后,原始查詢編寫起來會更簡單:

它變成了這個:

SELEct rng FROM myview WHERE wkday='Thursday' AND cd='DA6220';

沒有更多的OR,​​無需無休止地重復您正在尋找的代碼

...如果你想搜索多個代碼,它也是花生:

SELEct rng FROM myview WHERE wkday='Thursday' AND cd IN ('DA6220','IA6010');

但是你的新觀點很酷,你可以簡單地問:

我有代碼'DA6220'的日期或范圍是多少?

  SELEct wkday FROM myview WHERE cd='DA6220';

結果:

wkday
----------
Tuesday
Wednesday
Monday

范圍

  SELEct rng FROM myview WHERE cd='DA6220';

結果:

rng
-----
11-12
10-11
10-11

這是VIEW的典型應用。 它讓您的生活更輕松。

闡述我的評論:

SELECT *
  FROM `table`
 WHERE 0 < LOCATE("DA6220", CONCAT(
          `9-10`, " ", `10-11`, " ", `11-12`, " ",
          `12-1`, " ", `1-2`, " ", `2-3`, " ", `3-4`
       ))

這種方法的好處是你不必重復重復搜索字符串。 它可能比OR版本慢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM