![](/img/trans.png)
[英]PDOstatement (MySQL): inserting value 0 into a bit(1) field results in 1 written in table
[英]How to set the value of a field when inserting the results of a MySQL query into a table
我有以下查詢將其結果插入到表中:
INSERT INTO search_results (rec_type, id, name)
SELECT user_id AS id, name
FROM organizations
WHERE name LIKE '%Smith%'
GROUP BY user_id
ORDER BY name
我將如何在此查詢中為找到的所有記錄設置 rec_type 的值,'rec_type = "personel"',或者我是否必須進行第二次查詢並使用 UPDATE 來設置 rec_type 的值?
將字符串放入SELECT
列表中。
INSERT INTO search_results (rec_type, id, name)
SELECT 'personel' AS rec_type, user_id AS id, name
FROM organizations
WHERE name LIKE '%Smith%'
ORDER BY name
當您不使用任何聚合函數(例如MAX()
、 COUNT()
、 SUM()
)時,使用GROUP BY
毫無意義。 如果有重復的結果,請使用SELECT DISTINCT
來抑制它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.