MySQL 中 select 中的通配符

发布于 2024-10-31 03:53:15 字数 98 浏览 1 评论 0原文

有什么方法可以选择带有通配符的列。

想要

选择名称类型为“SELECT %type% from table_name”的列?

Is there any way to select columns with wild cards.

like

to select columns with names having type could be 'SELECT %type% from table_name' ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-11-07 03:53:15

并不真地。您可以使用*列通配符来选择所有列。如果要联接多个表,则可以通过在表名称或别名前加上 * 前缀来选择特定表中的所有列:

SELECT a.id, a.title, b.*
  FROM articles AS a
    JOIN blurbs AS b ON a.id = b.article

但是,除非您符合以下条件,否则不应使用 *:正在编写数据库管理程序。

或者,您可以通过获取表元数据来获取列名称,从而在 SQL 或其他语言中构建语句。仅使用 MySQL,您可以查询 COLUMNSINFORMATION_SCHEMA< 中的代码>表/code> 数据库获取列名称并使用 GROUP_CONCAT 为语句构建列列表。

SELECT CONCAT(
      'SELECT ',
      GROUP_CONCAT(COLUMN_NAME SEPARATOR ', '),
      ' FROM ', :db, '.', :table,
      ' WHERE ...'
      )
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA=:db AND TABLE_NAME=:table

将“:db”、“:table”和“...”替换为适当的值。您甚至可以将其转换为准备好的语句,以便可以将其用于任何表。从那里, PREPAREEXECUTE 构造的语句。

如果您不限于使用 SQL 进行编程,那么它应该不会那么混乱。您选择的语言的数据库驱动程序可能提供获取元数据的方法。实际的实现类似于纯 SQL 方法(获取列名、汇编语句、准备、执行),但不应该那么难看,因为您将使用算法语言,而不是声明性语言。

我非常有兴趣了解实际需要的情况。

Not really. You can use the * column wildcard to select all columns. If you're joining multiple tables, you can select all columns from specific table by prefixing * with the table name or alias:

SELECT a.id, a.title, b.*
  FROM articles AS a
    JOIN blurbs AS b ON a.id = b.article

However, you shouldn't use * unless you're writing a DB administration program.

Alternatively, you can build a statement within SQL or another language by fetching table metadata to get the column names. Using just MySQL, you can query the COLUMNS table in the INFORMATION_SCHEMA database to get the column names and use GROUP_CONCAT to build the column list for the statement.

SELECT CONCAT(
      'SELECT ',
      GROUP_CONCAT(COLUMN_NAME SEPARATOR ', '),
      ' FROM ', :db, '.', :table,
      ' WHERE ...'
      )
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA=:db AND TABLE_NAME=:table

Replace ":db", ":table" and "..." with the appropriate values. You can even turn it into a prepared statement so you can use it for any table. From there, PREPARE and EXECUTE the constructed statement.

If you're not limited to SQL for programming, it should be less messy. The DB driver for your language of choice likely offers methods to get metadata. The actual implementation would be similar to the pure SQL approach (get column names, assemble statement, prepare, execute), but shouldn't be so ugly, as you'd be using an algorithmic, rather than declarative, language.

I would be very interested in seeing the situation that this is actually required..

我的影子我的梦 2024-11-07 03:53:15

您可以使用 information_schema 然后使用准备好的语句找到名称中包含类型的所有字段。

set @str = (concat('select ',(select concat(group_concat(column_name),' from ',table_name)
from information_schema.columns
where table_schema = 'your_db_name' and table_name = 'your_table_name' and column_name like '%type%')));
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;

You can find all fields that contains type within the name using the information_schema and then using prepared statement.

set @str = (concat('select ',(select concat(group_concat(column_name),' from ',table_name)
from information_schema.columns
where table_schema = 'your_db_name' and table_name = 'your_table_name' and column_name like '%type%')));
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;
此岸叶落 2024-11-07 03:53:15

如果您使用前端语言当然可以。如果 php 只是使用

 $fieldlist= "cola, colb ";
 $tablename="tabl";
"select $fieldlist from $table"

我的直觉告诉你正在使用 php-mysql 做一些简单的事情,但我可能是错的。

Certainly possible if you are using a front-end language. If php just use

 $fieldlist= "cola, colb ";
 $tablename="tabl";
"select $fieldlist from $table"

My intuition is telling you are doing something simple using php-mysql but I may be wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文