简体   繁体   English

是否有可能在SQL中SELECT * FROM表WHERE列1 =某些东西而不是column_name = something

[英]Is it possible in SQL to SELECT * FROM a table WHERE the column 1 = something and not the column_name = something

I would like to SELECT * FROM table where the first column is equal to a variable. 我想SELECT * FROM table ,其中第一列等于变量。 It supposed that I don't know the column name. 它以为我不知道列名。

I know I can do something like 我知道我可以做点什么

SELECT * FROM table WHERE column_id = 1 

But I can't compare the data. 但我无法比较数据。

How can I do that? 我怎样才能做到这一点?

I found some solution with T-SQL but it doesn't interest me. 我找到了一些使用T-SQL的解决方案,但它对我不感兴趣。

To be more accurate : 为了更准确:

I'm developing an administration panel in my website where the "super" admin can directly modify the database. 我正在我的网站上开发一个管理面板,“超级”管理员可以直接修改数据库。 For that I can select a table and edit this table. 为此我可以选择一个表并编辑该表。 But to do that, I'm using an only PHP script which showing all tables, we can select one and the script show all rows in the selected table. 但要做到这一点,我使用的是一个只显示所有表的PHP脚本,我们可以选择一个,脚本显示所选表中的所有行。 After that you select a row and you are redirected to a page where the problem is. 之后,您选择一行,然后重定向到问题所在的页面。 This page can receive any table with only one row, so I want to SELECT the data contained in this row. 此页面可以接收任何只有一行的表,因此我想SELECT此行中包含的数据。

Images to understand: 要了解的图像:
The first one shows the tables. 第一个显示表格。
The second shows the rows of a selected table. 第二个显示所选表的行。
The third shows (normally) the data of 1 row but in this picture we can see data of many rows. 第三个显示(通常)1行的数据,但在这张图中我们可以看到许多行的数据。
selecto http://imageshack.us/g/135/selecto.png selecto http://imageshack.us/g/135/selecto.png

I found a solution : 我找到了解决方案:

Try to explain: First : I selected all form the specific table which was posted 试着解释一下:首先:我选择了所有已发布的特定表格

 $query="SELECT * FROM ".$_POST['table']."";
    $result=mysql_query($query);

Second: I attributed to a variable the column name (which I didn't know) 第二:我把一个变量归结为列名(我不知道)

while($fields=mysql_fetch_array($result))
    {
        $col =  mysql_field_name($result,0);
        $nb++;
    }

Third: I selected data from the table where $col = id of the row 第三:我从表格where $col = id选择了where $col = id行的数据

$sql = "SELECT * FROM ".$_POST['table']." WHERE ".$col."=".$_GET['idRow']."";
$result1=mysql_query($sql);

If you know how many columns there are, you could use this little trick here: 如果你知道有多少列,你可以在这里使用这个小技巧:

SELECT *
FROM (
  SELECT null x1, null x2, ..., null xn
  WHERE 1 = 0
  UNION ALL
  SELECT * FROM my_table
) t
WHERE t.x1 = something

In other databases than MySQL, renaming "unknown" columns would be even simpler, eg in PostgreSQL you could rename only the first column like this: 在MySQL之外的其他数据库中,重命名“未知”列会更简单,例如在PostgreSQL中,您只能重命名第一列,如下所示:

SELECT * FROM my_table t(x) WHERE x = something

If you don't know anything about the table 如果您对表格一无所知

... you can quickly query the information_schema first: ...您可以先快速查询information_schema

SELECT column_name
FROM information_schema.columns
WHERE table_name = :my_table
AND ordinal_position = 1

A note on SQL injection 关于SQL注入的注释

Please don't, DON'T do this. 请不要,不要这样做。 EVER: EVER:

$query="SELECT * FROM ".$_POST['table']."";

I've recently written an article about SQL injection . 我最近写了一篇关于SQL注入的文章 Every single vulnerability like yours will allow any script kiddie to dump your database, or worse. 像您这样的每个漏洞都会允许任何脚本小子转储您的数据库,或者更糟。

The solution is to sanitize your input first. 解决方案是首先清理您的输入。 Ideally, you'll maintain a catalog of allowed table strings, compare your $_POST variable with those, and then concatenate the pre-defined table string into your SQL statement, NOT the user input. 理想情况下,您将维护一个允许的表字符串目录,比较您的$_POST变量,然后将预定义的表字符串连接到您的SQL语句,而不是用户输入。

In PHP you could do something like: 在PHP中,您可以执行以下操作:

$col = 'users';
mysql_query("SELECT * FROM table WHERE $col = $something");

I think you can use SHOW CREATE TABLE table_name to fetch the schema of the table. 我认为您可以使用SHOW CREATE TABLE table_name来获取SHOW CREATE TABLE table_name的架构。 After that, you should already know every column. 之后,您应该已经知道每一列。

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

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