[英]Insert data in one table, remove from another, with timestamp condition in mysql
[英]MySQL : insert only if another table condition … AND retry if not
表
產品 | 審查 |
---|---|
product_id | product_id |
地位 | 作者 |
文本 |
我 select 最小和最大 product_id 的
$query_select = "SELECT MIN(product_id) as min_id, MAX(product_id) as max_id FROM product";
$res_select = $con->query( $query_select );
$row = $res_select->fetch_assoc();
$min_id = $row["min_id"];
$max_id = $row["max_id"];
我用隨機 product_id 在另一個表中輸入值
array loop
$query_insert = "INSERT INTO review ( product_id, author, text) VALUES (" . rand( $min_id, $max_id ) . ",'" . $data[ "name" ] . "','" . $data[ "commentary" ] . "')";
第一期
但僅在隨機product_id
status = 1
時插入(我不能在 query_select 中使用WHERE status = 1
和 MIN 和 MAX)
第 2 期
並且如果隨機 product_id status = 0
,請嘗試另一個status = 1
的隨機product_id
... 不中斷循環
我試過 INSERT SELECT, INSERT SELECT WHERE EXISTS, ...
不要使用單獨的查詢來獲取最小/最大產品 ID。 在INSERT... SELECT
中使用JOIN
與product
表。
$stmt = $con->prepare('
INSERT INTO review (product_id, author, text)
SELECT product_id, ?, ?
FROM product
WHERE status = 1
ORDER BY RAND()
LIMIT 1');
$stmt->bind_param("ss", $data["name"], $data["commentary"]);
然后在插入循環中調用$stmt->execute()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.