简体   繁体   English

SQLite 如果未达到限制,则从其他表中添加内容

[英]SQLite Add content from other table if limit has not been reached

I have a quick question.我有一个快速的问题。 I have two SQLite tables like the following:我有两个 SQLite 表,如下所示:

TableOne

id ID rowone罗万
1 1 rowone_content_a rowone_content_a
2 2 rowone_content_ab rowone_content_ab
3 3 rowone_content_ac rowone_content_ac
4 4 rowone_content_bc rowone_content_bc

TableTwo

id ID rowone罗万 rowtwo行二
1 1 rowone_content_a rowone_content_a a一个
2 2 rowone_content_ab rowone_content_ab ab抗体
3 3 rowone_content_ac rowone_content_ac ac交流
4 4 rowone_content_bc rowone_content_bc bc公元前

So I want a SQL that has the following behavior:所以我想要一个具有以下行为的 SQL:

Search: tent_a (something that contains tent_a somewhere).搜索: tent_a (某处包含tent_a的东西)。

When I search for it, I want the following result:当我搜索它时,我想要以下结果:

rowname that doesn't matter无关紧要的行名 from table (not included in the output)来自表(不包括在输出中)
a一个 TableTwo
ab抗体 TableTwo
ac交流 TableTwo
rowone_content_a rowone_content_a TableOne
rowone_content_ab rowone_content_ab TableOne

Background is that I have a search that should first look in the first row of the second table and select the value of the second row if the value of the first row contains the text I am looking for, limited by 5. But if the data returned from table two is less than 5, the SQL should fill the empty fields with data found in table one.背景是我有一个搜索应该首先查看第二个表的第一行和 select 第二行的值,如果第一行的值包含我要查找的文本,限制为 5。但是如果数据从表二返回小于 5,SQL 应使用表一中的数据填充空白字段。

Thanks for any help.谢谢你的帮助。

I think you want union all :我想你想要union all

select rowone, 'tableone'
from tableone
where rowone like '%tent\_a%'
union all
select rowtwo, 'tabletwo'
from tabletwo
where rowone like '%tent\_a%'

Use UNION ALL to get all the rows from the 2 tables that satisfy your conditions and then Use ORDER BY and LIMIT to return only the first 5 rows:使用UNION ALL从满足您的条件的 2 个表中获取所有行,然后使用ORDER BYLIMIT仅返回前 5 行:

SELECT rowname
FROM (
  SELECT rowtwo rowname, 1 fromtable
  FROM TableTwo
  WHERE rowone LIKE '%tent\_a%' ESCAPE '\'
  UNION ALL
  SELECT rowone, 2
  FROM TableOne
  WHERE rowone LIKE '%tent\_a%' ESCAPE '\'
)  
ORDER BY fromtable, rowname LIMIT 5

See the demo .请参阅演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只有在未达到限制时才插入MySQL - Mysql insert only if limit has not been reached SQL 服务器 - 内部错误:已达到表达式服务限制 - SQL Server - Internal error: An expression services limit has been reached 如果已达到数据库的最大限制,sql异常错误号是多少? - What's the sql exception error number if the maximum limit of the database has been reached 数据库的请求限制为30且已达到:此错误的请求计数的状态是多少? - The request limit for the database is 30 and has been reached: which statuses of request count for this error? 如何将一个表的 count() 添加到使用 SQLite 连接多个其他表的 SQL 查询中? - How do I add a count() for one table into a SQL query that has joins for multiple other tables using SQLite? SQL存储过程添加值并在达到最大值后停止 - SQL stored procedure to add up values and stop once the maximum has been reached 某些列已更新时不更新SQLite表行 - Not updating SQLite table row when a certain column has been updated 计算用户是否达到了舷梯限制 - Count if a user has reached the borrwing limit 如何限制已从数据库评估的文本 - How to make a limit on a text that has been eval from database 从哪个表中检索了数据 - From Which Table the data has been retrieved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM