4 回答

TA貢獻(xiàn)1844條經(jīng)驗 獲得超8個贊
用小括號()包含就可以區(qū)別開。
例如:
select * from table where title like '%hello%' and (contents like '%good%' or contents like '%ok%')
sql語句where部分解釋如下:
title like '%hello%' and (contents like '%good%' or contents like '%ok%')
title 字段模糊查詢包含 hello 字符串的數(shù)據(jù),并且 contents 字段模糊查詢包含 good 字符串的數(shù)據(jù),或者contents 字段模糊查詢包含 ok 字符串的數(shù)據(jù)
比如數(shù)據(jù)表數(shù)據(jù)如下:
字段 id --- title --- contents
數(shù)據(jù) 1 --- 11hello22 --- yougoodss
2 --- aaahello333 --- fdffokssfff
3 --- bbbhello666 ---- fffaafdafa1
像上面的數(shù)據(jù)sql語句會同時查詢出1、2的數(shù)據(jù)。
就像四則運(yùn)算加上小括號就有了計算優(yōu)先原則。

TA貢獻(xiàn)1828條經(jīng)驗 獲得超3個贊
一樣的寫法 只是PHP的標(biāo)準(zhǔn)的寫法是 where (`a`=1 or `a`=2) and `b`=3 這里的a b 為數(shù)字庫字段名.所以要用那個符號.有時候不用也不出錯.但為了更能適用多個平臺.所以建議還是寫上好點.

TA貢獻(xiàn)1818條經(jīng)驗 獲得超8個贊
select * from x where (id=1 and name="2") or (id=1 and name="3");
可以這樣寫
select * from x where id=1 and name in("2","3");
in 里面可以有很多參數(shù),如果in里面數(shù)據(jù)量比較大,可以用數(shù)組存儲,如
$array = (1,2,3,4,5);
$str = '';
foreach($array as $val){
$str .= $val . ','; //將值后面拼接一個逗號
}
$array = rtrim($str,','); //將數(shù)組變成合法的字符串
select * from x where id=1 and name in($str);
這樣也是批量操作的一個方法
- 4 回答
- 0 關(guān)注
- 1045 瀏覽
添加回答
舉報