mysql查詢語句and,or

2022-07-02 12:18:11 字數 891 閱讀 5772

where查詢裡,常用到and,or

and

select field1, field2,...fieldn from table_name1, table_name2...

where condition1 and condition2.....

or

select field1, field2,...fieldn from table_name1, table_name2...

[where condition1 [or] condition2.....

and 和 or

兩者一起用的時候,and的優先順序大於or

eg:查詢2023年11月5日到2023年11月11月之間,國家為中國或者美國的資訊

解決方法:將最小邏輯用括號括起來

//錯誤使用

select * from table_name where created_at > '2018-11-05 00:00:00' and created_at < '2018-11-12 00:00:00' and country = '中國' or country = '美國' //查詢出4000條資訊,錯誤

//正確使用

select * from table)_name where (created_at > '2018-11-05 00:00:00' and created_at < '2018-11-12 00:00:00') and (country = '中國' or country = '美國') //查詢出1400條資訊,正確

mysql 查詢語句

在pdo中有很多模式能用,在使用的時候在用 bindvalue 的時候 在select 中有in 的 語句無法實現,在傳入的時候 select from users where id in 1,2,3 當1,2,3 用 pdo param str 的時候,會出現這種情況 select from ue...

MySQL查詢語句

建立水果表 create table fruits f id char 10 not null,s id int notnull,f name char 255 not null,f price decimal 8,2 not null,primary key f id 插入資料 insert in...

MYSQL查詢語句

內連線 取的兩個表的 有能連線的字段 的交集,即欄位相同的。利用內連線可獲取兩表的公共部分的記錄。select st.sno,st.sname,st.s st.age,st.sdept,co.cname,sc.grade from student st,course co,score sc wher...