我有一个像这样的大桌子
user_id item value
1 aa 1
1 bb 0
1 cc 0
2 aa 1
2 bb 1
2 cc 1
2 xxx 1
3 aa 0
3 bb 0
3 cc 0
... ... ...
我要插入这样的记录
{user_id} xxx 0
对于所有没有此“ xxx”记录的用户。可以在SQL中执行此操作吗?
答案 0 :(得分:1)
您所要求的基本知识适合进行PATH /home/lab/conda/envs/dada2/bin:/home/lab/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD /home/balter/F1000_workflow/MicrobiomeWorkflow
R_ARCH
R_BROWSER /usr/bin/open
R_BZIPCMD /home/lab/conda/bin/bzip2
R_DOC_DIR /home/lab/conda/lib/R/doc
R_GZIPCMD /bin/gzip
R_HOME /home/lab/conda/envs/dada2/lib/R
R_INCLUDE_DIR /home/lab/conda/envs/dada2/lib/R/include/
R_LIBS_SITE /home/lab/conda/envs/dada2/lib/R/library/
R_LIBS_USER /home/lab/conda/envs/dada2/lib/
检查,首先,您可以选择没有该条目的用户,如下所示。
NOT EXISTS
然后,您可以构造您的插入语句。
SELECT DISTINCT u1.user_id
FROM yourUserTable u1
WHERE NOT EXISTS (SELECT 1 FROM yourUserTable u2 WHERE u2.item='xxx' and u2.user_id = u1.user_id)
如果user_id /项目上有唯一键,则有一些替代选项。