Forum Moderators: open

Message Too Old, No Replies

Getting Wrong Result

Mysql

         

surendra246

12:03 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



Hi Buddy!

I would like to Retrieve all value whatever match from our
Mysql Query give below..

select * from `tablename` where 1 AND `fields` in (1.0,1.5,1.6,1.9,143.0) ORDER BY `fields` ASC

but i am getting only

1.0
1.5
143.0

rows

i have also used

select * from `tablename` where 1 AND `fields` in ('1.0','1.5','1.6','1.9','143.0') ORDER BY `fields` ASC

but got same result

so plz give me any suggestion where I am wrong ....

I am appreciate for your support..

thank you

blang

1:43 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



This should be in the MySQL / database forum.

Are there records in the database a `field` column value of 1.6, 1.9?

What datatype is the column `fields`?

Why are you using 'where 1 AND `fields`' instead of simply selecting the column?

rocknbil

3:51 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



select * from `tablename` where 1 AND `fields` in ('1.0','1.5','1.6','1.9','143.0') ORDER BY `fields` ASC

"Where 1" is *usually* a dynamic programming approach when building a select. On it's own, it's only returns rows that are true; "select * from table where 1" is identical to "select * from table". For example,

$select = 'select * from table where 1';

(if some other condition exists, say an input from a search)
if ($keyword) { $select .= " and keyword like '%".$keyword."%'"; }
if ($username) { $select .= " and username ='".$username."'"; }

This is an easy way to develop a dynamic select, but as you see it's confusing reading the output. A more graceful approach:


if ($keyword) {
if ($where) { $where .= " and"; }
$where .= " keyword like '%".$keyword."%'";
}
if ($username) {
if ($where) { $where .= " and"; }
$where .= " and username ='".$username."'";
}
$select = "select * from table";
if ($where) { $select .= " where $where"; }

Eliminating "where 1" which is superfluous.

To answer the question, see blang's post, give us some examples of what is in the database that matches that field set and is not getting selected. Your select should "work" in either case. If the field is decimal, quotes are not needed, if it's varchar/text, quotes are needed.