开发者

select ... where id = any value. is it possible?

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


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.


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.


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`


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.


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`,'')


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.


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.


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

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


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.


$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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜