如何选择 MySQL 列中的所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11004925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:48:22  来源:igfitidea点击:

How do I select all rows in a MySQL column

mysql

提问by John Bowlinger

This seems to be an easy question but I can't for the life of me find an answer. How do I select all rows in a column regardless of value? For instance if I want everything in the column location:

这似乎是一个简单的问题,但我终其一生都无法找到答案。无论值如何,如何选择列中的所有行?例如,如果我想要列位置中的所有内容:

"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='I don't care, return everything'"

Keep in mind that I need location in there because I'm getting a dynamic URL query to this database, otherwise I would have just left it out.

请记住,我需要其中的位置,因为我正在获取对该数据库的动态 URL 查询,否则我会忽略它。

EditI've read some of the answers and shame on me for not making myself clearer. I'm trying to see if a MySQL query where you can select every row in a column is possible without the need for this:

编辑我已经阅读了一些答案,并为我没有让自己更清楚而感到羞耻。我正在尝试查看是否可以在不需要此操作的情况下进行 MySQL 查询,您可以在其中选择列中的每一行:

if ($location == '') {
  "SELECT * FROM whateverTable WHERE id='$id' AND date='$date'"
} else {
  "SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='$location'"
}

Rather than hacking up my code like that (because I have a hell of alot more query clauses in my real code), I'm trying to see if something like this is possible :

而不是像那样修改我的代码(因为我的真实代码中有更多的查询子句),我试图看看这样的事情是否可能:

"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='%'"

or this:

或这个:

"SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location='.*'"

But I'm not having any luck. If it's not, I'll just move on. Sorry for not making myself clearer

但我没有任何运气。如果不是,我会继续前进。抱歉没有让自己更清楚

回答by Mark Byers

As you said in your question, you should build the query dynamically and just omit that field. The query would then become:

正如您在问题中所说,您应该动态构建查询并省略该字段。然后查询将变为:

SELECT * FROM whateverTable
WHERE id = '$id'
AND date = '$date'

If you can't do that for some reason, you could try something like this:

如果由于某种原因你不能这样做,你可以尝试这样的事情:

SELECT * FROM whateverTable
WHERE id = '$id'
AND date = '$date'
AND (location='$location' OR '$location' = '')

Setting $locationto the empty string will cause all locations to be returned.

设置$location为空字符串将导致返回所有位置。

And don't forget to correctly escape your strings to avoid SQL injection attacks.

并且不要忘记正确转义您的字符串以避免 SQL 注入攻击。

回答by hafichuk

SELECT location FROM whateverTablewill return you every single row/value in the location column.

SELECT location FROM whateverTable将返回位置列中的每一行/值。

Add your where clause if you want to start filtering the results down.

如果要开始过滤结果,请添加 where 子句。

回答by JustinDanielson

If you're dynamically building the query just check if location is null or the empty string and then omit that part of the query.

如果您正在动态构建查询,只需检查 location 是否为 null 或空字符串,然后省略查询的那部分。

回答by Carlos

Replace equal symbol : "=" by LIKE

替换等号:“=” LIKE

SELECT * FROM whateverTable WHERE id='$id' AND date='$date' AND location LIKE '%'

回答by C_Sutt

I think you're looking for a sub query?

我想你正在寻找一个子查询?

SELECT * FROM whateverTable WHERE location IN (SELECT location FROM whateverTable);