简体   繁体   English

如何在MySQL的“ FROM”子句上使用“ LIKE”功能

[英]How to use “LIKE” function on “FROM” clause in MySQL

I am trying to query a specific table based on a user input, and want to query the table that contains that user input as the search_id in the name. 我正在尝试根据用户输入查询特定的表,并想查询包含该用户输入的表作为名称中的search_id。 For example, the table names are structured as run_000000_search_000000_lda_000000 and I want to get the table that has the same search_id as the on they put in. 例如,表名称的结构为run_000000_search_000000_lda_000000 ,我想获取具有与它们放入的search_id相同的表。

I know when "LIKE" is used on the Where clause you can get a column with a specific string or int in the name. 我知道在Where子句上使用“ LIKE”时,您可以获得名称中包含特定字符串或int的列。 However, I have found nothing on using it with the "FROM" clause. 但是,我没有发现将其与“ FROM”子句一起使用。

Specific hardcoded query for search_id 50 针对search_id 50的特定硬编码查询

SELECT *
FROM `run_000044_search_000050_lda_000366`;

In summary, how can I get the table that has the same search_id as the one the user put in. Also trying to use the run and lda to form a full table name does not work because there is no noticeable pattern in between the 3 names. 总之,如何获取与用户输入的表具有相同search_id的表。另外,尝试使用run和lda来形成完整的表名是行不通的,因为这三个名称之间没有明显的模式。

I also can use dynamic sql but am not sure how to do it. 我也可以使用动态sql,但不确定如何执行。

After searching online I have finally found out a solution. 在网上搜索后,我终于找到了解决方案。 The infomation_schema.tables contains a column will a table names. infomation_schema.tables包含一列将是一个表名。 From here you can query the full name of the table and use that in another query. 在这里,您可以查询表的全名,并在另一个查询中使用它。 I think that a full example makes it much easier to understand: 我认为,完整的示例可以使您更容易理解:

SELECT * FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE '%_search_000050_lda_%';

SELECT * FROM INFROMATION_SCHEMA.TABLES

^ selects table will containing all table names and ^选择表将包含所有表名和

WHERE TABLE_NAME LIKE '%_search_000050_lda_%';

^ search all tables for whats in the quotes. ^在所有表中搜索引号中的内容。 Replace the '%_seach_000050_lda_%' with the Like statement you need for the table 用表所需的Like语句替换'%_seach_000050_lda_%'

LIKE is usually used in WHERE clause after FROM for example 例如,LIKE通常用在FROM之后的WHERE子句中

SELECT * FROM table_name WHERE field LIKE 'value';

you can also compare number or int field by '=' operator which return a specific record. 您还可以通过'='运算符比较数字或整数字段,以返回特定记录。

SELECT * FROM table_name WHERE id = 30;

if you want extract details from specific record set of another table you can subquery method for example refer this link 如果要从另一个表的特定记录集中提取详细信息,则可以使用子查询方法,例如,请参考此链接

you can also use dynamic sql query.refer this link for dynamic sql query 您也可以使用动态SQL查询。请参考此链接进行动态SQL查询

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM