gpt4 book ai didi

sql - 从选择查询中查询插入

转载 作者:行者123 更新时间:2023-12-04 20:42:56 25 4
gpt4 key购买 nike

我有一个名为 tb_tweetkurator_test 的表,查询是:

SELECT id_user,
COUNT(id_tweet) as JML_TWEET,
COUNT(IF(url = 'yes', url, NULL)) as JML_TWEET_URL,
COUNT(IF(mention = 'yes', mention, NULL)) as JML_TWEET_MENTION,
COUNT(IF(retweet = 'yes', retweet, NULL)) as JML_TWEET_RETWEET,
COUNT(IF(tweet_biasa = 'yes', tweet_biasa, NULL)) as JML_TWEET_BIASA
FROM tb_tweetkurator_test
GROUP BY id_user;

结果是

id_user   | JML_TWEET | JML_TWEET_URL| JML_TWEET_MENTION | JML_TWEET_RETWEET | ML_TWEET_BIASA

16912985 | 20 | 6 | 13 | 0 | 3
23343960 | 10 | 10 | 0 | 0 | 0
46651600 | 20 | 9 | 17 |15 | 2
267164011 | 20 | 14 |10 |4

让我感到困惑的是,我可以将上面的数据(来自 SELECT 查询)插入到新表中吗?

最佳答案

是的。您可以使用 create-as-select 从这个结果集中创建一个新表:

CREATE TABLE new_table
SELECT id_user,
COUNT(id_tweet) as JML_TWEET,
COUNT(IF(url = 'yes', url, NULL)) as JML_TWEET_URL,
COUNT(IF(mention = 'yes', mention, NULL)) as JML_TWEET_MENTION,
COUNT(IF(retweet = 'yes', retweet, NULL)) as JML_TWEET_RETWEET,
COUNT(IF(tweet_biasa = 'yes', tweet_biasa, NULL)) as JML_TWEET_BIASA
FROM tb_tweetkurator_test
GROUP BY id_user;

或者将其插入到现有表中:

INSERT INTO new_table
SELECT id_user,
COUNT(id_tweet) as JML_TWEET,
COUNT(IF(url = 'yes', url, NULL)) as JML_TWEET_URL,
COUNT(IF(mention = 'yes', mention, NULL)) as JML_TWEET_MENTION,
COUNT(IF(retweet = 'yes', retweet, NULL)) as JML_TWEET_RETWEET,
COUNT(IF(tweet_biasa = 'yes', tweet_biasa, NULL)) as JML_TWEET_BIASA
FROM tb_tweetkurator_test
GROUP BY id_user;

关于sql - 从选择查询中查询插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com