Select 多表 SQL 无连接

Select in multiples tables PSQL without join

(如有错误请指正)

我有 2 个表:

我创建了一个脚本来删除不在数据库中的图像,问题是: 我不知道如何检查 activity 的内容和同一请求的文章,因为下面的请求只是删除我的活动图像..

#!/bin/bash

db="intranet_carc_development"
user="benjamin"

echo "DELETING UNUSED FILES AND IMAGES..."
for f in public/uploads/files/*
do
  if [[ -f "$f" ]]
  then
    f="$(basename "$f")"
    psql $db $user -t -v "ON_ERROR_STOP=1" \
    -c "select content from public.articles where content like '%$f%'" | grep . \
    && echo "exist" \
    || rm public/uploads/files/$f
  fi
done
printf "DONE\n\n"

如果绑定类似:

select content from public.articles, public.activities where content like '%$f%'" 

但是我有这个日志错误

ERROR:  column reference "content" is ambiguous

你可以试试

WITH artcontent AS (
    SELECT content
    FROM public.articles
),
actcontent AS (
    SELECT content
    FROM public.activities
),
merge AS (
    SELECT * FROM artcontent 
    UNION ALL
    SELECT * FROM actcontent 
)
SELECT *
FROM merge

UNION ALL 语句会将您的两个结果 artcontent(来自文章)和 actcontent(来自活动)放在一起。

希望对您有所帮助!