473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL Less Than returns wrong results

I have an sql query which has several fields and no matter how hard i try i cant get the less than statement to work....i originally tried between but that wouldnt work either.

$yourpricesearch is the name of the drop box with in the search form with the price values of 5.00, 10.00, 15.00 etc...i did it this way as i wasn't sure if when i put the AND in when the value of $yourpricesearch was empty would cause problems so im only putting the statement in if $yourpricesearch has a value
my prices of the books are in the format 10.15 for example

Expand|Select|Wrap|Line Numbers
  1. if(!$yourpricesearch==""){$yourprice=" AND yourprice < '$yourpricesearch'";}
  2. //Now we search for our search term, in the field the user specified 
  3. $sql = "SELECT * FROM mytable_name WHERE status = 'active' AND isbn LIKE '%$isbnsearch%' AND title LIKE '%$keyword%' AND topic LIKE '%$topicsearch%' AND university LIKE '%$universitysearch%' $yourprice";
it still doesnt even seem to work if i put the less than statement directly into the sql query......also between statement didnt work.

im not sure if something is wrong in the statement?

sorry forgot to say how it returns wrong results.....at the momwent there are book prices ranging from 2.00 to 19.00 ish.....when you search for less than 20.00...they all show.....less than 15.00 they all show.....less than 10.00....none show...yet there are some with 5.00.....2.00 etc?

thanks
Feb 19 '09 #1
5 3438
Atli
5,058 Expert 4TB
Hi.

Try looking at the query exactly like it is before it gets executed.
You can do that by simply printing the $sql variable in the code you posted.

It would also help to see the CREATE TABLE statement you used to create you table.
Feb 20 '09 #2
Hi,

I printed the query and this is what is print.....all looks fine to me.....when i search for less than 20.00 they all show even the 2.99 one...yes when i search < '10.00' nothing shows...not sure what the issue is?

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM iswapbooks_books WHERE status = 'active' AND isbn LIKE '%%' AND title LIKE '%%' AND topic LIKE '%%' AND university LIKE '%%' AND yourprice < '10.00'
the only thing i thought it could be was my bookid column is the primary key on auto increment and it is currently on 75 ish but theres only about 16 items in the table as i've deleted loads....could that be the issue...should i maybe reset my auto increment value...if so can i do this without deleting the items in the table.

Cheers
Feb 20 '09 #3
Atli
5,058 Expert 4TB
I can't see anything wrong with the query, although without knowing the exact structure I can't really say anything with any certainty.

What is the type of the "yourprice" column?
And what version of MySQL are you using?

@ukfusion
This shouldn't matter in the slightest.
Gaps in a auto_increment Primary Key column have no effect on the data in the table.
Feb 20 '09 #4
Hi...the yourprice field is VARchar...so this could me my problem...its mysql 4 however i can alter it to 5.....but i think i have to re-create the database...dont think it lets me just upgrade it...which could cause problems?
Feb 20 '09 #5
Atli
5,058 Expert 4TB
Ok, I see now.

The problem is that you are using incorrect types to store your data.

The price you are storing is a number, yet you are storing it in a VarChar field, which is a string type.
And to top that of, the value you use to compare it to is quoted, which means that even tho it looks like a number, MySQL considers it to be a string.

There are two probable solutions. Either should work, but doing both would be best.

#1
Your price field should be a numeric type. That way MySQL can actually evaluate it's value as a number and determine correctly whether or not it is actually higher or lower than the value you use to compare it with.

For monetary values, the DECIMAL type is usually best. It allows you to specify exactly how many whole numbers you want and how many fractions.

You could change that by simply doing:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE `iswapbooks_books` 
  2.     CHANGE `yourprice` (
  3.         `yourprice` Decimal(7,2) Not Null
  4.      );
Before you do this!.. make sure you back up any data you do not want to lose.
Even tho I highly doubt this will cause any damage to you data, there is always a remote possibility that something could go wrong when altering tables.

#2
The value you use to compare with the price is a string. This should be a number.

By that I mean: this part of your query:
Expand|Select|Wrap|Line Numbers
  1. yourprice < '10.00'
Because the 10.00 is wrapped in single-quotes, MySQL assumes it is a string rather than a number.
This can cause problems when comparing the value with actual numbers, or strings that look like numbers, as the two do not follow the same rules when compared.

To fix this, simply alter the query so it looks like this:
Expand|Select|Wrap|Line Numbers
  1. yourprice < 10.00
Feb 20 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: lkrubner | last post by:
www.php.net says: >>>>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of...
14
by: Salad | last post by:
A97. Situation: I have 3 tables with a text field in each and a date field in the first 2 tables: Table1 Text1, Date1 Table2 Text2, Date2 Table3 Text3 (no date field) The following...
7
by: Douglas Buchanan | last post by:
I cannot access certain column values of a list box using code. I have a list box 'lstPrv' populated by the query below. SELECT tblPrv.fkPrvID, lkpCat.CatNm, lkpSrv.SrvNm, lkpCat.pkCatID,...
2
by: Matt Michael | last post by:
I'm using ADO .NET to connect to a Microsoft Access database, and everything so far has been working in my program except one small problem. I am constructing a query string to search for certain...
9
by: ward | last post by:
Good morning. I created an edit_task page that allows the user to edit tasks for the database. I entered some text and used some hard returns. Put when I went to view the tasks (using a PHP...
13
by: inetquestion | last post by:
I've narrowed the code to a problem with php/iplanet on linux. When I run the following code from the command line "$status" comes back as "0" as it should. However when I hit it with a browser,...
3
by: baka | last post by:
Dear Sir/madam Here i am having some one sql which returns more rows than the required rows after comaparing tables simple sql statement will be lokk like SELECT t.empcd, s.daicd, s.chucd,...
0
by: ASP Developer | last post by:
I have a web service that returns a class when a web method is called. This class has a enum property with four values. These four values have default numbers. For example, Apple = 5 Orange...
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.