簡體   English   中英

將MySQL查詢的結果插入表中時如何設置字段的值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM