各位前輩想請問一下
我有兩個table a和b
裡面的欄位名稱都一樣
a.table 有資料 b.table 沒資料
但我每次執行都要從a.table 隨機選取一筆存進b.table
而且 不能和b.table裡面的資料重覆
但是我用left join卻沒辦法得到我要的效果
想請問各位前輩 這個sql要怎麼下呢?
然後隨機選取出來還有echo出來!
這是我的sql指令
$sql="select * from vote1 as a left join vote as b on a.name!= b.name and a.uid != b.uid and a.idcard != b.idcard order by rand() //limit 1"
可以使用 NOT IN
<pre class="c" name="code">SELECT * FROM a WHERE a.uid NOT IN (SELECT uid FROM b) ORDER BY rand() LIMIT 1
語法沒測試過,不過應該是對的0。0
SELECT *
INTO b
FROM (
SELECT *
FROM a
WHERE a.uid NOT IN (
SELECT b.uid
FROM
b
)
ORDER BY RANDON() LIMIT 1,1
);