使用通配符选择以 XXX 开头的所有列?

使用通配符选择以 XXX 开头的所有列?

问题描述:

我的数据库中有几列名称相似.我如何根据它们开头的单词选择它们?这是一个示例表格布局:

I have several columns in my databases with similar names. How do I select those based on the word they start with? Here's an example table layout:

我尝试使用

$Food = "Vegetable"; 
mysql_query("SELECT `" . $Food . " %` FROM `Foods`");

但它似乎不起作用.

任何帮助将不胜感激:-)

Any help would be appreciated :-)

显然,从我的示例中并不清楚这一点,但我已经知道所有专栏的第一句话.列始终相同,并且不会添加或删除食物种类".PHP 变量仅用于确定我需要的几种集合类型中的哪一种.

没有办法完全按照您的意愿去做.您可以先执行另一个查询以获取所有列名,然后在 PHP 中处理它们并构建第二个查询,但这可能比仅写出您想要的名称更复杂.

There's no way to do exactly what you're trying to. You could do another query first to fetch all the column names, then process them in PHP and build the second query, but that's probably more complex than just writing out the names that you want.

或者这个查询需要动态的原因是什么?表的结构会经常变化吗?

Or is there a reason this query needs to be dynamic? Will the table's structure change often?