MySQL返回不良结果

时间:2018-11-26 19:18:35

标签: mysql sql

我对SELECT FROM WHERE语句有疑问,该查询返回错误的结果。

这是我的桌子,被称为朋友:

+----------+-----------+------------+--------+--------+-------+
| lastname | firstname | callprefix | phone  | region | zip   |
+----------+-----------+------------+--------+--------+-------+
| Lužný    | Bob       |        602 | 111222 | OL     | 79821 |
| Matyáš   | Bob       |        773 | 123456 | BR     | NULL  |
| Strouhal | Fido      |        300 | 343434 | ZL     | 76701 |
| Přikryl  | Tom       |        581 | 010101 | PL     | 72000 |
| Černý    | Franta    |        777 | 000999 | OL     | 79801 |
| Zavadil  | Olda      |        911 | 111311 | OL     | 79604 |
| Berka    | Standa    |        604 | 111234 | ZL     | 72801 |
| Vlcik    | BbB       |        736 | 555444 | KV     | 35210 |
+----------+-----------+------------+--------+--------+-------+

这是我的查询。

SELECT * FROM friends WHERE region <= 'z';

我希望具有区域ZL的行应该存在,但不存在。你能告诉我为什么吗?

结果是:

+----------+-----------+------------+--------+--------+-------+
| lastname | firstname | callprefix | phone  | region | zip   |
+----------+-----------+------------+--------+--------+-------+
| Lužný    | Bob       |        602 | 111222 | OL     | 79821 |
| Matyáš   | Bob       |        773 | 123456 | BR     | NULL  |
| Přikryl  | Tom       |        581 | 010101 | PL     | 72000 |
| Černý    | Franta    |        777 | 000999 | OL     | 79801 |
| Zavadil  | Olda      |        911 | 111311 | OL     | 79604 |
| Vlcik    | BbB       |        736 | 555444 | KV     | 35210 |
+----------+-----------+------------+--------+--------+-------+

当我尝试此查询时:

SELECT * FROM friends WHERE region >= 'z';

结果包含region ='ZL'的两行

????

谢谢!

3 个答案:

答案 0 :(得分:0)

因为“ ZL”大于“ Z”。 Z只是一个字符,因此只会返回小于Z或具有Z值的值。您要通过此查询尝试实现什么?

答案 1 :(得分:0)

  

你能告诉我为什么吗?

如果添加区域为object(stdClass)#65 (1) { ["WSI2_RecherchePointRelaisResult"]=> object(stdClass)#64 (11) { ["STAT"]=> string(1) "0" ["PR01"]=> object(stdClass)#56 (8) { ["Num"]=> string(6) "098517" ["LgAdr1"]=> string(31) "LA DIFF " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "RUE COURMONT 18 " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR02"]=> object(stdClass)#5 (8) { ["Num"]=> string(6) "001424" ["LgAdr1"]=> string(31) "BOULANGERIE HUBINON LAMONT " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "67 RUE D ISLY " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR03"]=> object(stdClass)#67 (8) { ["Num"]=> string(6) "072109" ["LgAdr1"]=> string(31) "LES HALLES DE MOULINS " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "40-42 RUE JEAN JAURES " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR04"]=> object(stdClass)#68 (8) { ["Num"]=> string(6) "098428" ["LgAdr1"]=> string(31) "O MARCHE DES BISCOTTES " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "RUE DE MARQUILLIES 33 " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR05"]=> object(stdClass)#69 (8) { ["Num"]=> string(6) "002528" ["LgAdr1"]=> string(31) "MY AUCHAN WAZEMMES " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "14 PLACE NOUVELLE AVENTURE " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR06"]=> object(stdClass)#70 (8) { ["Num"]=> string(6) "005393" ["LgAdr1"]=> string(31) "CAR PRO SERVICES " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "103 RUE BALZAC " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR07"]=> object(stdClass)#71 (8) { ["Num"]=> string(6) "003124" ["LgAdr1"]=> string(31) "IMEDIAPHONE " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "94 RUE RACINE " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR08"]=> object(stdClass)#72 (8) { ["Num"]=> string(6) "002742" ["LgAdr1"]=> string(31) "EPICERIE SUPER 400 " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "364 RUE DU FAUBOURG D ARRAS " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR09"]=> object(stdClass)#73 (8) { ["Num"]=> string(6) "098425" ["LgAdr1"]=> string(31) "AUX DELICES " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "RUE D INKERMANN 26. " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59000" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } ["PR10"]=> object(stdClass)#74 (8) { ["Num"]=> string(6) "002898" ["LgAdr1"]=> string(31) "PHONECOM SERVICES " ["LgAdr2"]=> string(0) "" ["LgAdr3"]=> string(31) "273 RUE PIERRE LEGRAND " ["LgAdr4"]=> string(0) "" ["CP"]=> string(5) "59800" ["Ville"]=> string(26) "LILLE " ["Pays"]=> string(2) "FR" } } } 的记录,并按区域的字母顺序对这些行进行排序,您是否希望foreach($pointsRelais as $value) { if( is_object($value) && !empty(trim($value->Num)){ var_dump($value->Num); // does'nt work } } Z之前或之后?显然它将接after而来,因此不符合您的标准。

如果您只想考虑第一个字符,请将其添加到您的条件中

ZL

如果您的数据库设置要求区分大小写,我也将Z明确地用大写字母表示。

答案 2 :(得分:0)

您尝试过吗 SELECT * FROM friends WHERE region <= 'zl';

从计算机的角度来看,'z' < 'zl'