Wrong result from query - php

I have this simple query
SELECT *
FROM `book`
WHERE `BookID` = '7u'
LIMIT 1
i expect empty result
But i see one result with book id =7.
BookID is auto increment.
Why query ignore 'u' character?

Because 7u is not numeric, so apparently mysql is ignoring the u.
Maybe you are thinking of some high level programming languages which use suffixes to qualify the type of number? In C-derived languages, 7u would be an unsigned integer with value 7.

Just because of two reasons:
There may be only one record.
you were using LIMIT 1. That display only one record from the output. so use LIMIT 5 OR 2.

the mysql takes the string as 7u so when you try to do this stuff it coverts in a string but mysql is not in strict mode so it coverts string into integer because your id is intenger and its rounding of 7u becomes 7 thats why its display the one record try without quote 7u instead of '7u' then it gives error

Related

Sql returns empty row on combinning primary key with another value in where clause

I am getting empty row on following sql
SELECT * FROM flyers WHERE fId='6' AND 'userId'='400'
MySQL returned an empty result set
(i.e. zero rows). (Query took 0.0004 sec)
But when i use
SELECT * FROM flyers WHERE fId='6'
Showing rows
0 - 0 (1 total, Query took 0.0005 sec).
As i got my result and that is correct using only primary key.
But i does not know why mysql returns empty row on using AND with primary key.
Note:- fId is flyer table primary key.
Try this:
Use back ticks for the column name not single quotes
If userid is int data type it means you have to remove single quotes userid=400
If fid is string it means fID='6'
SELECT * FROM flyers WHEREfId=6 ANDuserId=400
In your query you have only one mistake, which is that you used single quotes around userId. Use back-ticks instead or nothing:
SELECT * FROM flyers WHERE fId='6' AND userId='400'
SELECT * FROM flyers WHERE fId=6 AND userId=400// safe not to use quotes
But I suggest not to use quotes around numbers for below reason.
The real issue comes down to type casting. When you put numbers inside quotes, it is treated as a string and MySQL must convert it to a number before it can execute the query. While this may take a small amount of time, the real problems start to occur when MySQL doesn't do a good job of converting your string.
For example, MySQL will convert basic strings like '123' to the integer 123, but will convert some larger numbers, like '18015376320243459', to floating point. Since floating point can be rounded, your queries may return inconsistent results.

How to check if a column value is in a string SQL

I have a database with 150,000 records and I need to match its FULL column value or records, with some parts of the string.
**
As i want to check if the STRING contains the COLUMN records and NOT!
if the COLUMN contains the string
**
(Example below)
For testing purposes
Lets say the database has a TABLE , 1 COLUMN and 1 record as the records are similar to this:
come to me
and i need to match it with this #STRING
She wants to come to me now
I want to execute something similar to :(but this doesn't work of course)
SELECT * from TABLE where #STRING like '%'+COLUMN+'%'
I can't seem to solve this with SQL the usage of PHP is possible but prefer if the solution is with SQL but if the solution with PHP is available please propose it and note that the database has over 150,000 records
SELECT * from TABLE where LOCATE(COLUMN, #STRING)>0;
LOCATE(a,b) returns a number giving the character location of a in b, or returns 0.
See Mysql LOCATE()
The docs discuss that LOCATE() is only case sensitive when one of the strings is a 'binary string'. That probably doesn't affect your use case, though if it became an issue you could CONVERT() the binary strings to a locale and use LOWER() to get lower case.
MySQL String Functions
The dynamic like syntax for mysql is
SELECT * from TABLE where #STRING like CONCAT('%',COLUMN,'%')

Mysql type hidden type casting

I am trying to improve an old system (written ages ago) where every mysql queries are glued from string. So example of that query looks like
"SELECT * FROM User WHERE id > '3'"
Id column is of course bigint and PK.
What does mysql do with '3' in query where id should be a int value? I assume it is treated as a string (due to '') so this value is casted into int during analyze/optimize process by mysql. Am I right?
//UPDATE
I probably asked wrong way. There are two way to handle it.
(Fast) Mysql automatically detects that id should be int and rewrite/cast a query to
SELECT * FROM User WHERE id > 3
before send it to DB engine
(Unbelievable) Mysql does
SELECT * FROM
then in loop apply condition WHERE id > '3' and cast it for EVERY row
I just want to be sure that second option is impossible.
MySQL will always cast the string to a number for comparing, which in this case this is the right thing to do (it will use the index on the column to find the values).
If your column is a string and you compare it to an integer constant MySQL will cast the column to an integer and not use the index.
MySQL will automatically cast it into the correct column type. If it cannot for some reason, it will throw an error.
Make sure to use prepared statements with PDO or MySQLi in any case where the parameter may come from an unsafe source (the user, an external API).

Find Max numeric value of two varchar fields

I'm trying to find the max value contained in two separate fields of my table.
The code I'm using in my model is:
$query = $this->db->query("select max($field1) as max_id_1 from default_table1");
$row1 = $query->row_array();
$query = $this->db->query("select max($field2) as max_id_2 from default_table1");
$row2 = $query->row_array();
return max($row1['max_id_1'], $row2['max_id_2']);
I'm a complete novice where PHP and CodeIgniter is concerned - as I'm sure my code demonstrates :)
It is working insofar as it's returning values, but not the maximum values I have in the fields. For instance I know there is a 4000 value but the highest returned is 750.
I'm wondering if this is because the fields are of type VARCHAR because although they predominantly contain numbers there are some that contain characters (- or &) or the word 'to' so I couldn't use the INT type.
Because of using VARCHAR is it failing to see that 4000 is larger than 750?
If so is there a way to cast the field contents as integer before checking for the max value, and will this be affected by the non-integer values in the fields?
All offers of help and advice is gratefully received.
Tony.
This can be done using SQL using MySQL's implicit type conversion:
select max(case when (field1+0)>(field2+0) then field1+0 else field2+0 end)
from default_table1
Using +0 would convert varchar to number and also ignore any characters that follow after the number. If you still need the original content, you can write the query like this:
select case when (field1+0)>(field2+0) then field1 else field2 end
from default_table1
order by case when (field1+0)>(field2+0) then field1+0 else field2+0 end desc
limit 1
Oh, oops, I've forgot the mysql part
You should do the same thing, cast to integer:
select max(cast($field1 to unsigned)) as max_id_1 from default_table1
It depends of your data, but you may try something like that
return max((int)$row1['max_id_1'], (int)$row2['max_id_2']);
Have a look to the PHP doc on string to int conversion
What you are really asking for is a way for codeigniter to convert the strings to numbers, and the find the max values of those. I don't know of any way to do this in codeigniter.
If you really want this, you have two options:
loop through all the rows in the table, and use php to parse out the number, while looking for the maximum number
Add a number column to the db, and do the string-to-number parsing in this new column whenever the values are inserted.
The first option is incredibly inefficient, and the second is probably your best bet.

Order By problem with int Mysql

I have prices in a database as INT if I order by I get
97,650
ends of appearing before
975,455
after sorting in DESC
It appears that since the 6 in the first price is greater than the 3rd place position in the second price, it counts that as a larger number.
Has anyone run into this problem and know how to solve it?
To test, try this slow and sad query.
select prices from (table) order by (prices+0);
If that query works, then it is likely your column is not an INT column. The (prices+0) makes certain that the data is treated as an integer.
Once you have found that this does indeed work for you, you need to alter the column to be an INT column.
You should change the field type from varchar to int.

Categories