如何检查给定数据是否存在于多个表中(所有表都具有同一列)? [英] How to check if a given data exists in multiple tables (all of which has the same column)?

查看:47
本文介绍了如何检查给定数据是否存在于多个表中(所有表都具有同一列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表,每个表由称为用户名的列组成.在注册部分,我需要检查所请求的用户名是否是新的和唯一的.

I have 3 tables, each consisting of a column called username. On the registration part, I need to check that the requested username is new and unique.

在继续之前,我需要一个SQL来告诉我该用户是否存在于任何这些表中.我试过了:

I need that single SQL that will tell me if that user exists in any of these tables, before I proceed. I tried:

SELECT tbl1.username, tbl2.username, tbl3.username
FROM tbl1,tbl2,tbl3
WHERE tbl1.username = {$username}
   OR tbl2.username = {$username}
   OR tbl3.username ={$username}

那是路吗?

推荐答案

select 1 
from (
    select username as username from tbl1
    union all
    select username from tbl2
    union all
    select username from tbl3
) a
where username = 'someuser'

这篇关于如何检查给定数据是否存在于多个表中(所有表都具有同一列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆