Query =
SELECT * FROM options WHERE 'key' = 'exchange_rate'
Table structure =
key varchar(500) latin1_swedish_ci
value text latin1_swedish_ci
Table content
Key | Value
exchange_rate | 25
Program
phpMyAdmin 4.0.4 Mysql
Result
0 Rows returned
Do you know what the problem is here?
try this query:
SELECT * FROM options WHERE `key` = 'exchange_rate'
instead of :
SELECT * FROM options WHERE 'key' = 'exchange_rate'
Qoutes are used to show varchar values not column name
SELECT * FROM options WHERE `key` = 'exchange_rate'
Related
I have 4cci and 4ccI values in database. When I run this query I get both rows.
select * from urls where url = "4cci";
How to do a case sensitive search?
Depend on your text field collation
Check the manual
mysql> SET #s1 = 'MySQL' COLLATE latin1_bin,
-> #s2 = 'mysql' COLLATE latin1_bin;
mysql> SELECT #s1 = #s2;
+-----------+
| #s1 = #s2 |
+-----------+
| 0 |
+-----------+
Use keyword BINARY before the column you want to perform case sensitive search
Description :
The BINARY function converts a value to a binary string.
Example
Suppose we have one table stores with 3 records where two recoard have country column as "IN" and one has as "in"
select count(*) from stores where BINARY country = 'in';
Output : count : 1
OR
Use CAST() function :
Description :
Convert a value to a DATE datatype:
select count(*) from stores where CAST(country AS BINARY) = 'IN';
Output : count : 2
How these function works :
Understand with simple example :
SELECT BINARY "HELLO" = "hello";
Output : 0
Here MySQL performs a byte-by-byte comparison of "HELLO" and "hello" and return 0 (because on a byte-by-byte basis, they are NOT equivalent):
How can I select from database by given string and mysql to return the row on same string with diacritics?
select * from myTable where nume = "Stefan"
and the row from database that should be returned:
id = 1
name = "Ștefan"
You can search like:
SELECT * from myTable
where
CONVERT(nume USING utf8) LIKE '%Stefan%'
But this is a bit unclear, as MySql already know how to search for strings with diactritics.
I am trying to update a field to put relevant data in the location column, so it will not be blank. In this case, I would like to copy airport_address into the location column.
airport_address is varchar(500)
location is varchar(100)
I would like to copy the 1st 100 char of airport_address into location
Any idea why this SQL would not be working?
This returns 563 rows:
SELECT *
FROM tbl_events
WHERE location LIKE ""
This affects 0 rows:
UPDATE tbl_events
SET location = LEFT(airport_address, 100)
WHERE location LIKE ""
This affects 0 rows:
UPDATE tbl_events
SET location = airport_address
WHERE location LIKE ""
Try this:
UPDATE tbl_events
SET location = LEFT(airport_address, 100)
WHERE
( location IS NULL )
OR
( LTRIM(RTRIM(location)) = '' )
suppose the user input
mysite.com/profile?identity=1
mysite.com/profile?identity=dinodsja
mysite.com/profile?identity=1a
getting the value
$identity = $_GET['identity']; // identity can be user_id or user_name
and i have a simple select query:
SELECT * FROM lb_users WHERE (user_id = 'dinodsja' OR user_name = 'dinodsja') AND user_status = 1
and it works fine. but the problem is:
SELECT * FROM lb_users WHERE (user_id = '1a' OR user_name = '1a') AND user_status = 1
when I execute this query it also returns the result without satisfying the condition.
Table structure:
user_id bigint(25)
user_name varchar(50) utf8_general_ci
**
-> Is this a MySQL Bug ?
-> How can we avoid this ?
-> What will be the query ?
**
The reason for that is because the data type of the column user_ID is integer.
MySQL silently drops any trailing NON-Number (and anything that follows within) in the value and that is why 1a is equal to 1 since a will be remove in the value.
SQLFiddle Demo
I do remember having a similar problem long ago.
First some background: This is not a bug. It is actually a feature. Ok, it's one that might lead to such unexpected behaviour, but MySQL is thereby very tolerant w.r.t. user inputs, respective select queries:
mysql> SELECT 'a' = 'a ';
-> 1
mysql> SELECT 'A' = 'a';
-> 1
Therefore, with implicit type conversion, the result of, e.g, '1a' in INTEGER is 1, but also:
mysql> SELECT 0 = 'x6';
-> 1
mysql> SELECT 1 = ' 1';
-> 1
mysql> SELECT 1 = ' 1a';
-> 1
This feature is also implemented in other not statically typed languages. PHP, for instance, calls this type juggling. See the PHP String conversion rules and this example from the documentation:
<?php
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
?>
See JavaScript:
<script>
document.write(parseInt("40 years") + "<br>");
</script>
=> 40
Nevertheless, the solution to your problem is pretty easy: Just cast the integer to a char and do the comparison then:
mysql> SELECT * FROM lb_users WHERE (CAST(user_id AS CHAR) = '1' OR user_name = '1')
-> 1
mysql> SELECT * FROM lb_users WHERE (CAST(user_id AS CHAR) = '1a' OR user_name = '1a')
-> 0
mysql> SELECT * FROM lb_users WHERE (CAST(user_id AS CHAR) = 'dinodsja' OR user_name = 'dinodsja')
-> 1
I made a fiddle for everyone to try it out: http://sqlfiddle.com/#!2/c2835/14/0
Hope that helps,
-Hannes
According to your previous message
its a user input for profile. user can provide user_id or user_name.
so the input is valid. but no data. – DBK Mar 30 at 6:42
I'd recommend testing to see if its an integer and only search the user ID if it's an integer. It's really more of a workaround for mySQL not handling a STRING to INT comparison, but it should work.
declare #InputVar varchar(10)
set #InputVar = '1a'
SELECT *
FROM lb_users
WHERE
(case when isnumeric(#InputVar) = 1 then
case when (user_id = #InputVar OR user_name = #InputVar) then 1 else 0 end
else
case when user_name = #InputVar then 1 else 0 end
end =1 )
And
user_status = 1
When dealing with strings I would use 'LIKE' instead of '=' to avoid this silent type conversion madness. LIKE is made to work with strings so why not use it.
SELECT * FROM lb_users WHERE (user_id = '1a' OR user_name = '1a') AND user_status = 1
you get 1 result if you change '1a' to 1a you get this:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1a LIMIT 0, 30' at line 1
This is not a bug, take a look at http://dev.mysql.com/doc/refman/5.0/en/where-optimizations.html
hope this helps
I think you cannot duplicate a primary key and an ID, I test that one and i come up with a running data..did you set the user_id with its attributes like:
user_id bigint(50) auto_increment primary key
this is not a mysql error.
I got the following query :
INSERT INTO contracts_settings (contract_id, setting_id, setting_value)
VALUES (:contract_id, (
SELECT setting_id
FROM settings
WHERE setting_type = :setting_type
AND setting_name = :setting_name
LIMIT 1
), :setting_value)
ON DUPLICATE KEY UPDATE setting_value = :setting_value
The value with the prefix : is replaced with data using PHP PDO::bindBalue.
If the inner query find nothing (it return NULL) but also INSERT a NULL statement. How to avoid that ?
Thanks.
Convert the INSERT ... VALUES syntax to INSERT ... SELECT:
INSERT INTO contracts_settings
(contract_id, setting_id, setting_value)
SELECT
:contract_id,
setting_id,
:setting_value
FROM settings
WHERE setting_type = :setting_type
AND setting_name = :setting_name
LIMIT 1
ON DUPLICATE KEY UPDATE
setting_value = :setting_value ;