选择...其中 id = 任何值。是否可以?

发布于 2024-09-02 17:27:02 字数 560 浏览 5 评论 0原文

请看这个表,

table
|id| |name| |order|

我必须获取行,其中 name = Somethingorder = somevalue

所以我编写

select `id` from `table` where `name` = 'something' and `order` = 'somevalue'

但取决于 php 逻辑,有时我需要获取所有行,其中name = some,与order值无关。我不想改变查询结构,因为实际上有很多字段,并且可能的查询数量会变得非常大。所以我想保存查询的结构,当我需要仅按名称选择时,我想写这样的内容:

select `id` from `table` where `name` = 'something' and `order` = any value 

可能吗?

谢谢

look at this table please

table
|id| |name| |order|

i must get the rows, where name = something and order = somevalue

so i write

select `id` from `table` where `name` = 'something' and `order` = 'somevalue'

but depend on php logic, sometimes i need to get all rows, where name = something, independently of order value. i don't want to change the query structure, because in practise there are many number of fields, and possible count of queries will become very big. so i want to save the structure of query, and when i need to select just by name, i want to write something like this:

select `id` from `table` where `name` = 'something' and `order` = any value 

is it possible?

thanks

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

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

发布评论

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

评论(10

黯然 2024-09-09 17:27:02

嗯,这有点像黑客,但如果你真的需要这样做,它会像这样工作:

select `id` from `table` where `name` = 'something' and `order` = `order`

然后你只是说“只要顺序与其本身相同”,所以它总是正确的。

Well, it's kind of a hack, but if you really need to do this, it'll work like this:

select `id` from `table` where `name` = 'something' and `order` = `order`

Then you're just saying "wherever order is the same as itself", so it's always true.

初相遇 2024-09-09 17:27:02

不,这是不可能的。您需要更改结构(可以选择更改为 LIKE,以便您可以使用“%”,但这非常难看)。

但是,您不需要编写不同的查询来处理每种可能的组合。您可以简单地动态创建查询:

//create base query
$query = "select `id` from `table` where `name` = 'something' ";

//add order if we need it
if ($use_order)
  $query .= "and `order` = 'somevalue' ";

//repeat for any other optional part

请注意,您当然仍然应该采取适当的措施来避免 SQL 注入和其他安全问题 - 为了简单起见,我没有将其包含在此处。

No, this is not possible. You need to change the structure (optionally to a LIKE so you can use '%', but that's very ugly).

However, you don't need to write a different query to handle every possible combination. You can simply create the query dynamically:

//create base query
$query = "select `id` from `table` where `name` = 'something' ";

//add order if we need it
if ($use_order)
  $query .= "and `order` = 'somevalue' ";

//repeat for any other optional part

Note that you should of course still take proper measures to avoid SQL injection and other security issues - I have not included this here in order to keep things simple.

勿忘初心 2024-09-09 17:27:02

如果你使用绑定参数,这是不可能的。

如果您只是替换这些值,则可以执行以下操作:

select `id` from `table` where `name` = 'something' and `order` = `order`

If you are using bound parameters, it would be impossible.

If you just substitute the values, you can do the following:

select `id` from `table` where `name` = 'something' and `order` = `order`
暗地喜欢 2024-09-09 17:27:02

这是数据库查询的常见主题 - 您需要一个变量查询,具体取决于您希望对其查询的数据应用多少过滤。您可以采用在整个代码中将查询作为字符串重复的路线,但这是不好的做法,因为它不必要地增加了代码的复杂性。如果您出于某种原因需要更改查询,并且因此必须在多个位置进行更改,则可能会出现错误。

更好的解决方案是创建一个函数来构建您执行的查询:

function buildMyQuery($name, $order = null) {
    $sql = "SELECT `id` FROM `table` WHERE `name`='$name'";

    if ($order != null) {
        $sql .= " AND `order`='$order'";
    }

    return $sql;
}

然后您可以仅使用“名称”字段来运行此函数:

$query = buildMyQuery("somename");

或者使用两个字段来运行此函数:

$query = buildMyQuery("somename", "someorder");

正如上面提到的,此代码被故意简化并且不包含通过 $name 或 $order 传入的可能存在危险的数据的意外情况。您需要在函数开始时使用 mysql_real_escape_string 或类似的东西先清理数据,然后再使用任何一条数据。

正如拜伦所说,动态查询生成是生活中的一个事实,所以我现在会习惯它,而不是使用黑客式的解决方法。

This is a common theme with database queries - you need a variable query depending on how much filtering you wish to apply to the data it queries. You could go the route of having your query repeated as a string throughout your code, but that is bad practice as it increases the complexity of the code needlessly. Chances for errors occur if you need to change the query for some reason, and have to change it in multiple places as a result.

The better solution is to create a function which builds the query for you execute:

function buildMyQuery($name, $order = null) {
    $sql = "SELECT `id` FROM `table` WHERE `name`='$name'";

    if ($order != null) {
        $sql .= " AND `order`='$order'";
    }

    return $sql;
}

You could then run this for just using the 'name' field:

$query = buildMyQuery("somename");

Or this for using both fields:

$query = buildMyQuery("somename", "someorder");

As someone mentioned above, this code is deliberately simplified and contains no contingency for possibly dangerous data passed in via $name or $order. You would need to use mysql_real_escape_string or something similar to clean the data first, at the beginning of the function before either piece of data is used.

Dynamic query generation is a fact of life as Byron says, so I would become accustomed to it now rather than using hack-ish workarounds.

风铃鹿 2024-09-09 17:27:02

我不认为你有任何选择......一旦你做了选择,你就不能“取消过滤”并获得更多行。

您应该只使用两个查询——两个独立的查询,或者一个将名称选择到临时表中的查询,然后(可选)一个进一步选择订单属性的查询。

I don't think you have any choice... Once you do a selection you can't "unfilter" and get more rows.

You should just use two queries-- either two independent queries, or one that selects on the name into a temp table, and then (optionally) one that further selects on the order attribute.

我的鱼塘能养鲲 2024-09-09 17:27:02

就像查德上面所说的那样,只需将列设置为其自身即可。但要小心,在某些平台/安装配置上,NULL != NULL:

select `id` from `table` where `name` = 'something' and coalesce(`order`,'') = coalesce(`order`,'')

Like Chad said above, just set the column equal to itself. But be careful, on some platforms / install configurations, NULL != NULL:

select `id` from `table` where `name` = 'something' and coalesce(`order`,'') = coalesce(`order`,'')
记忆里有你的影子 2024-09-09 17:27:02

经过反思,我有一个更好的答案。我的同事向我展示了一种可以做到这一点的方法。

我的例子...

Select rentals.* From rentals Where ((? = '') OR (user_id = ?))

变量必须相同。

例如,如果它们都是 5,则对于用户 id 为 5 的行,第一个布尔值将为 false,但第二个布尔值将为 true。

如果您需要“all”,则设置为空字符串将导致所有行都为看来满足where 子句条件。

On reflection, I have a better answer. My colleague showed me a way this can be done.

My example...

Select rentals.* From rentals Where ((? = '') OR (user_id = ?))

The variables must be the same.

If they are both 5 for example, the first boolean will be false, but the second will be true, for the rows where the users id is 5.

If you require "all", setting as an empty string will result in all rows being seen to meet the where clause condition.

删除→记忆 2024-09-09 17:27:02

您不能在此处使用 not null 查询吗?

select `id` from `table` where `name` = 'something' and `order` is not null;

Can't you just use a not null query here?

select `id` from `table` where `name` = 'something' and `order` is not null;
国际总奸 2024-09-09 17:27:02

您应该能够这样做:

select `id` from `table` where `name` <>'' and `order` <>''

这将选择值不等于空白的任何位置。

You should be able to do it like this:

select `id` from `table` where `name` <>'' and `order` <>''

That will select anywhere that the value is not equal to blank.

山人契 2024-09-09 17:27:02
$sql = "SELECT * FROM auctions WHERE id = id ";

if ($category !== "ANY") { 
$sql .= "AND category = $category "; }

if ($subcategory !== "ANY") { 
$sql .= "AND subcategory = $subcategory "; }

if ($country !== "ANY") { 
$sql .= "AND country = $country "; }



$sql .=  "ORDER BY $order $sort LIMIT $limit OFFSET $offset";
$sql = "SELECT * FROM auctions WHERE id = id ";

if ($category !== "ANY") { 
$sql .= "AND category = $category "; }

if ($subcategory !== "ANY") { 
$sql .= "AND subcategory = $subcategory "; }

if ($country !== "ANY") { 
$sql .= "AND country = $country "; }



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