MYSQL。使用列筛选提取数据

时间:2013-12-30 04:16:56

标签: mysql

我有一张这样的桌子:

mysql> select * from users;
+--------+----------+------------+-----------+
| userid | username | password   | privilege |
+--------+----------+------------+-----------+
|      1 | user1    | password   |         1 |
|      2 | david    | goodboy    |         1 |
|      3 | admin    | mastermold |         5 |
|      4 | user4    | password4  |         1 |
|      5 | user5    | password5  |         2 |
|      6 | user6    | password6  |         1 |
|      7 | user7    | password7  |         1 |
+--------+----------+------------+-----------+
7 rows in set

现在,如何通过唯一的选择查询提取名为“david”的用户名密码而不存储“密码”,结果在一个字段中。 (不接受“select * from users”)?

3 个答案:

答案 0 :(得分:0)

尝试

select password from users where username='david';

答案 1 :(得分:0)

嗯,你的意思是你的查询结果不应该有字符串“password”,如果我已经正确使用它。即使我发布了答案,你应该考虑@MarcB评论并尝试首先学习SQL。

您的查询应该是这样的

select `password` from users where username='david' 
and lower(`password`) not like '%password%';

答案 2 :(得分:0)

没有提到“密码”? 好吧,你可以将列名称抓取到一个变量中,然后像sqlFiddle

那样使用它
SET @columnName = (SELECT COLUMN_NAME FROM
                   INFORMATION_SCHEMA.COLUMNS
                   WHERE TABLE_NAME = 'users' 
                   AND ORDINAL_POSITION = 3);
SET @sql = CONCAT("SELECT ",@columnName," FROM users WHERE username = 'david'");
prepare statement FROM @sql;
execute statement;