mysql取兩個資料表其中一條資料

2021-10-02 02:23:16 字數 571 閱讀 5225

需求:根據特定條件先去取a表的資料,如果a表不存在就取b表的資料;

mysql語法為:

select * 

case when h.start_time is null then g.start_time else h.start_time end start_time,

case when h.end_time is null then g.end_time else h.end_time end end_time

from attn_grade_opening_hours g

left join attn_class_opening_hours h

limit 1

上面語句省略了很多表,只是展示了關鍵語句,核心是case語法。

從mysql資料表中隨機取出一條記錄

核心查詢資料表 select from 表名 order byrand limit 1 此處的1就是取出資料的條數 但這樣取資料網上有人說效率非常差的,那麼要如何改進呢 搜尋google,網上基本上都是查詢max id rand 來隨機獲取資料。select from table as t1 joi...

一條SQL 刪除兩個表中的資料

點乙個刪除按鈕的時候,刪除user1表的code x列的一行,而且刪除另乙個表user1中usercode列值等於x的所有行 因為我是初學,不會儲存過程和什麼觸發器.所以用的笨方法。我用的是拼接字串,其中最主要的就是在sql delete from user1 where code usercode...

某個字段重複,只取其中一條資料

使用分析函式row number over partiion by order by 來進行分組編號,然後取分組標號值為1的記錄即可。目前主流的資料庫都有支援分析函式,很好用。select u.from select row number over partition by id order by ...