mysql複雜查詢的書 mysql 複雜查詢

2021-10-19 19:19:24 字數 2763 閱讀 4538

1.同乙個表下多次查詢:

sql語句: select b.* ,(select name from exh_common.medicine_type a where b.p_id = a.id) as p_name from exh_common.medicine_type b;

獲取表medicine_type中所有內容,並獲取表中,所有p_id不為空的父型別的名稱

2.三表聯合查詢

sql: select a.*,b.type_id, c.`name`,c.description from medicine_cadn a left join medicine_cadn_type_rel b on a.id = b.cadn_id left join medicine_type c on c.id = b.type_id

三張表的表結構:

1.tablename:'medicine_goods'

id = column(string(40), primary_key=true)

cadn_id = column(string(40))

cadn_name = column(string(200))

cadn_py_name = column(string(200))

name = column(string(200), nullable=false)

py_name = column(string(200), nullable=false)

description = column(string(1020))

factory = column(string(200), nullable=false)

certificate = column(string(100), nullable=false)

label = column(integer, nullable=false)

drug_type = column(string(10), nullable=false, server_default=text("'固態'"))

drug_unit = column(string(20), nullable=false, server_default=text("'mg'"))

specification = column(string(200))

specification_shape = column(string(200))

specification_dose = column(string(200))

specification_count = column(string(200))

take_way = column(string(200))

images = column(text)

key1 = column(string(200))

val1 = column(string(200))

key2 = column(string(200))

val2 = column(string(200))

key3 = column(string(200))

val3 = column(string(200))

key4 = column(string(200))

val4 = column(string(200))

key5 = column(string(200))

val5 = column(string(200))

enabled = column(integer, nullable=false, server_default=text("'1'"))

is_del = column(integer, nullable=false, server_default=text("'0'"))

created = column(datetime, nullable=false)

created_by = column(string(40), nullable=false)

updated = column(datetime)

updated_by = column(string(40))

2.tablename__ = 'medicine_type'

id = column(string(50), primary_key=true)

name = column(string(100), nullable=false, unique=true)

description = column(string)

p_id = column(string(32))

enabled = column(integer, nullable=false, server_default=text("'1'"))

is_del = column(integer, nullable=false, server_default=text("'0'"))

created = column(datetime, nullable=false)

created_by = column(string(40), nullable=false)

updated = column(datetime)

updated_by = column(string(40))

3.tablename__ = 'medicine_cadn_type_rel'

id = column(integer, primary_key=true)

cadn_id = column(string(40), nullable=false)

type_id = column(string(40), nullable=false)

mysql查詢錯誤 複雜的MySQL查詢錯誤的結果

我正在嘗試構建複雜的 mysql查詢,但它返回錯誤的結果 select b.name as batch name,b.id as batch id,count distinct s.id as total students,coalesce sum s.open bal 0 as open bala...

複雜條件的查詢

1 主要內容 where子句中幾種複雜的搜尋條件,包括組合的搜尋條件 and,or in 運算子,not運算子,like運算子和相關的萬用字元。2 例項表 簡單查詢一文中的teacher表,和下面的course表 teacher表 course表 3 and 和or eg 使用teacher表,查詢...

Spring boot JPA的複雜查詢

就很坑這個東西搞了乙個下午的時間,終於搞會怎麼用 對於想要查詢 a and b or c 或者 a and b and c or d 在jpa裡你不能直接用and和or把字段拼起來,因為他並不會幫你自動加上括號,經過一下午的努力,學到了兩種方法,但是我只用了其中一種,另一種感覺emmm挺複雜的而且效...