I have two tables
我有两张桌子
tbl1:
id
userid
subscriptionid
column2
column3
tbl2:
id
userid
subscriptionid
column4
column5
Column6
Column7
The subscriptionid values are unique, and would only exist in tbl1 or tbl2, but may also be null
订阅ID值是唯一的,并且仅存在于tbl1或tbl2中,但也可以为空
I need a query to find a subscriptionid in either tbl1 or tbl2
我需要一个查询来查找tbl1或tbl2中的订阅ID
SELECT * FROM tbl1, tbl2 WHERE (tbl1.subscriptionid OR tbl2.subscriptionid) = 'some_val';
更多回答
Do tbl1
and tbl2
have the same schema definitions? Do they really only have one column each?
Tbl1和tbl2是否具有相同的模式定义?它们真的只有一栏吗?
No, they do not have the same schema, and have multiple columns. I updated the post - Thanks
不,它们没有相同的架构,并且有多个列。我更新了帖子-谢谢
I tried several queries using UNION's and cannot get it right, but I am sure you are correct
我尝试了使用Union的几个查询,但都不能正确回答,但我相信您是正确的
Just normalize the SELECT
list between the two queries in the UNION ALL
:
只需标准化Union all中两个查询之间的选择列表:
SELECT id, userid, subscriptionid, column2, column3, NULL AS column4, NULL AS column5, NULL AS column6, NULL AS column7
FROM tbl1 WHERE subscriptionid = ?
UNION ALL
SELECT id, userid, subscriptionid, NULL AS column2, NULL AS column3, column4, column5, column6, column7
FROM tbl2 WHERE subscriptionid = ?
Here's a db<>fiddle.
这是一把db<>小提琴。
You can achieve this by using the SQL UNION operator to combine the results from both tables and then filter for the specific value you're looking for. Here's how you can write the query:
您可以通过使用SQL UNION运算符将两个表的结果组合在一起,然后筛选您要查找的特定值来实现这一点。下面是如何编写查询的方法:
SELECT subscriptionid FROM tbl1 WHERE subscriptionid = 'some_val'
UNION
SELECT subscriptionid FROM tbl2 WHERE subscriptionid = 'some_val';
This query will return a result set that includes the subscriptionid values from either tbl1 or tbl2 where the subscriptionid matches the specified value 'some_val'. The UNION operator ensures that duplicate values are removed from the result set, and you'll get a unique list of matching subscriptionid values from both tables.
此查询将返回一个结果集,其中包含来自tbl1或tbl2的订阅ID值,其中订阅ID与指定值‘SOME_VAL’匹配。UNION操作符确保从结果集中删除重复的值,并且您将从两个表中获得匹配的订阅ID值的唯一列表。
I hope this solves your problem..
我希望这能解决你的问题。
更多回答
Beat me to my edit! OP should consider normalizing these two tables in their overall database design for better performance and scalability in the long-term. Short of that, adding indexes on both tables’ subscriptionid
columns may help as well.
抢在我之前编辑我的作品!OP应该考虑在总体数据库设计中对这两个表进行标准化,以获得更好的性能和长期的可伸缩性。除此之外,在两个表的订阅ID列上添加索引也可能有所帮助。
Agreed (there is a slight whiff), but the uniqueness of subscriptionid
across these two tables suggests some kind of typing. Perhaps the subscriptionid
should be PK in both tables and userid
should exist only in subs
table, but without knowing more about OP's db it is impossible to be sure.
同意(有一点),但是这两个表中subscriptionid的唯一性表明了某种类型的类型。也许subscriptionid应该在两个表中都是PK,而userid应该只存在于subs表中,但如果不了解OP的db,就不可能确定。
Never seen db fiddle before Will mess with that from now on
从未有过的db小提琴将从现在开始搅乱它
This returns just the word subscriptionid
这只返回单词SUBSCRIPTIONID
我是一名优秀的程序员,十分优秀!