簡體   English   中英

MySQL從左連接表中插入一列的所有行?

[英]MySQL insert into ALL rows of one column from left joined table?

我在INSERT語句中遇到困難,我提供了前四列的值。 第五個(最后一個)列應該連接另一個表的兩列。 那部分工作正常,但只有一行。 兩個表都有12行(因此是12個字面值),但是如何使連接的子查詢返回我試圖插入的一列的所有行 (從而與其他列中的值對應)?

嘗試了很多方法,但這就是現在的樣子......

    INSERT INTO properties (property_id, condo_type, pets, internet, owner_name)
      SELECT 
    '301S', '207S', '1100T', '1201S', '317T', '110S', '1010S', '409T', '505T', '1005S', '656S', '942S' AS property_id,
    'SandsOF3BR', 'SandsOF3BR', 'SandsOF2BR', 'Tides3BR', 'SandsOF2BR', 'Tides2BR', 'SandsOF2BR', 'SandsOF2BR', 'Tides2BR', 'Tides3BR', 'SandsOF2BR', 'SandsOF3BR' AS condo_type,
    1,1,0,0,0,0,0,1,1,1,1,0 AS pets,
    1,1,0,1,1,1,0,1,0,1,0,0 AS internet,
    CONCAT(owner_first_name, ' ', owner_last_name) AS owner_name
      FROM owners LEFT JOIN properties 
        ON owners.property_id = properties.property_id
      WHERE owners.property_id = properties.property_id;

所以基本上,屬性表和所有者表都在各自的“property_id”列中有數據,反映哪個屬性屬於什么所有者。 我的意思是,我是否必須創建一個循環遍歷第二個表的過程,還是有更傳統的方法? 或者我是否會填寫這個專欄? 請幫忙,謝謝......任何人?

INSERT INTO properties
            (property_id,   /*here*/
             condo_type,    /*you*/
             pets,          /*name*/
             internet,      /*5*/
             owner_name)    /*columns*/

SELECT '301S',              /*here you name way more than the 5 columns*/
       '207S',              /*from your insert statement.*/
       '1100T',              
       '1201S',             /*So, where to put them? MySQL doesn't know*/
       '317T',              /*That's why you get an error.*/
       '110S',
       '1010S',
       '409T',
       '505T',
       '1005S',
       '656S',
       '942S'                                         AS property_id,
       'SandsOF3BR',
       'SandsOF3BR',
       'SandsOF2BR',
       'Tides3BR',
       'SandsOF2BR',
       'Tides2BR',
       'SandsOF2BR',
       'SandsOF2BR',
       'Tides2BR',
       'Tides3BR',
       'SandsOF2BR',
       'SandsOF3BR'                                   AS condo_type,
       1,
       1,
       0,
       0,
       0,
       0,
       0,
       1,
       1,
       1,
       1,
       0                                              AS pets,
       1,
       1,
       0,
       1,
       1,
       1,
       0,
       1,
       0,
       1,
       0,
       0                                              AS internet,
       Concat(owner_first_name, ' ', owner_last_name) AS owner_name
FROM   owners
       LEFT JOIN properties
              ON owners.property_id = properties.property_id
WHERE  owners.property_id = properties.property_id;  

這兩列是什么

       Concat(owner_first_name, ' ', owner_last_name) AS owner_name

從業主表? 如果是,那么為什么要加入properties表呢? 沒有必要。

寫這樣:

INSERT INTO properties
            (property_id,
             condo_type,
             pets,
             internet,
             owner_name)
SELECT 'value1',
       'value2',
       'value3',
       'value4',
       Concat(owner_first_name, ' ', owner_last_name)
FROM   owners
;  

這將從所有者(當然只有2列)中插入所有行到屬性中。 前4列對於每一行都是相同的,因為它們只是字符串。

如果要將靜態字符串插入表中,可以編寫多個語句,也可以在一個語句中指定多行,如下所示:

INSERT INTO foo (col1, col2) VALUES
(1, 2), /*first row*/
(3, 4); /*second row*/

編輯:

要結合這兩種方法,你必須寫下這樣的東西:

 INSERT INTO properties
            (property_id,
             condo_type,
             pets,
             internet,
             owner_name)
SELECT 
       CASE owners.property_id WHEN 1 THEN 'value1'
                               WHEN 2 THEN 'value2'
       END AS column1,
       CASE WHEN owner_first_name = 'fancy' AND owner_last_name = 'pants' THEN 'value3'
       ELSE 'value4' END AS column2,
       Concat(owner_first_name, ' ', owner_last_name) AS owner_name
FROM   owners
       LEFT JOIN properties
              ON owners.property_id = properties.property_id
WHERE  owners.property_id = properties.property_id;  

不太實際;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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