Is it ok to enclose all values in SQL statement with single quotes ? For example:
This is simple table called books:
id | title
1 | Some book name
2 | Second book name
Is it OK to write statement like this:
SELECT * FROM books WHERE id = '1'
I've tested that query in SQL server 2008 and MySQL 5 and it works good, but I am curious is there any performance issue, because ID field is acctualy integer.
And second question is it OK to write statement like this:
SELECT * FROM books WHERE id = N'1'
N prefix is used in SQL server for UTF-8 fields, but I've tested that in SQL server and MySQL and both worked OK. I don't know if SQLite support N prefix, because I didn't test that.
The reason why I am asking this is because I am building database class that will work with popular SQL databases (SQL Server, MySQL, SQLite and maybe MS Access), so when performing selecting, inserting or updating data I don't have to worry about field datatype. I can always enclose value with N'Some value', but I am curious is this correct and is there any performance issues?
In SQL Server, you can get an implicit conversion when doing this. Sometimes it won't affect the plan or have noticeable impacts on performance, but sometimes it might. They are generally deemed to be bad. Inspect the plans for these two queries against AdventureWorks2012:
SELECT * FROM Sales.SalesOrderHeader WHERE SalesOrderID = 43659;
SELECT * FROM Sales.SalesOrderHeader WHERE SalesOrderID = '43659';
The latter contains a CONVERT_IMPLICIT (hover over the tooltip for the CI seek).
You also want to be very careful about switching between VARCHAR and NVARCHAR - in some cases this can be dreadful for performance, depending on the underlying data type and whether the literal has the N prefix.
Long story short: don't do this. Write your ORM thingy so that it understands the different data types and handles them appropriately.
SELECT ... WHERE int_type = '123' is fine to do. SQL will convert '123' to an integer once and be done with it.
However, SELECT ... WHERE char_type = 123 is no okay, because SQL will have to convert every cell into an integer. See that char_type = '0123' and char_type = '00123' will also match. So it has to do more work.
Thanks to #MartinSmith for pointing out a resource for precedence in casting: http://msdn.microsoft.com/en-us/library/ms190309.aspx
Here's an example bug in MySQL that says the implicit typecast from quoted string of digits to an integer causes a severe performance issue: http://bugs.mysql.com/bug.php?id=43319
It's best to not quote numbers.
SQL Server and MySQL both perform implicit type conversions, but it doesn't make it a good practice; use the appropriate native type when you can. This will let you avoid trying to spot the difference between:
SELECT * FROM books WHERE id = '1'
and
SELECT * FROM books WHERE id = 'l'
and
SELECT * FROM books WHERE id = 'O'
and
SELECT * FROM books WHERE id = '0'
(that's a one, a lower case L, a capital 'o', and a zero respectively)
Related
I am having trouble using mySQL IN() when using it with a number, rather than a field name.
My query is:
$query = "select rental_id from {$this->table_name}
where rental_id='$unit_id' AND '49' IN (specifications)";
The field specifications contains 20,10,49,22,18
An explanation states,
Note: #1003 /* select#1 */ select '270' AS `rental_id` from `mydatabase`.`rental_buildings` where 0
and shows and Extra of Impossible WHERE noticed after reading const tables
If I remove the single quotes from the 49, I get
Warning: #1292 Truncated incorrect DOUBLE value: '20,10,49,22,18'
Note: #1003
/* select#1 */ select '270' AS `rental_id`
from `mydatabase`.`rental_buildings`
where (('270' = '270') and (49 = '20,10,49,22,18'))
This seems like it should be very straightforward, but I can't get it to work. I'd appreciate any guidance
You can use FIND_IN_SET:
SELECT rental_id FROM ...
WHERE rental_id=? AND FIND_IN_SET(?, specifications)
Keep in mind this will be exceptionally slow and extremely punishing on datasets of non-trivial size as this must search through each field individually for all entries for that particular rental_id.
To fix this you need a proper relational table where your query would look like:
SELECT DISTINCT rental_id FROM specifications WHERE id=?
For that particular specification_id. This can be indexed and can be really fast.
I've been using the following format to insert into my mysql database previously, and would like to keep it uniform.
INSERT INTO OPENQUERY (ENET, 'SELECT * FROM ActiveDirectory.Computers') -- MYSQL database
SELECT c.[CommonName],
c.[DistinguishedName],
c.[SAMAccountName],
c.[DNSHostName],
c.[Location],
c.[Division],
c.[Department],
c.[ManagedBy],
c.[MachineRole],
CAST(CAST(c.[LastLogon] as timestamp) as datetime) AS LastLogon,
c.[OperatingSystem],
c.[OperatingSystemVersion],
c.[ServicePack],
c.[OU],
c.[CreatedOn],
c.[ChangedOn],
CASE WHEN c.[UserAccountControl] & 2 = 0 THEN 1 ELSE 0 END AS [Enabled] -- Check to see if disabled. Disabled = bitwise of 2; 4098 = 4096 + 2 = Trust Account + Disabled
FROM [IT_ActiveDirectory].[dbo].[ADComputerTable] c -- MSSQL database
My problem comes when inserting the fields that are datetimes in MSSQL (c.LastLogon, c.CreatedOn, c.ChangedOn) into mysql fields that are also datetimes. I have tried almost every combination of CAST() and CONVERT() I can think of, but I may have missed something. I have also tried changing mysql's field type to timestamp.
It returns: Conversion failed when converting date and/or time from character string.
It seems strange to me that MSSQL won't just send the data to MYSQL and let it do the conversion. Instead it looks like it is trying to convert and match the datatypes and data before it sends it to MYSQL.
If I can't insert it this way, I am open to another format, like if its possible to do the insert inside of the OPENQUERY() SELECT. Any suggestions? I'm dead in the water at the moment.
I would try to convert to ISO-8601 format string:
INSERT INTO OPENQUERY (ENET, 'SELECT * FROM ActiveDirectory.Computers')
SELECT
...,
CONVERT(VARCHAR, LastLogon, 126) AS LastLogon
FROM [IT_ActiveDirectory].[dbo].[ADComputerTable];
Also I don't like SELECT * in OPENQUERY. If columns are matched by position it is error-prone. Consider expanding start to columns.
This case is similar to: S.O Question; mySQL returns all rows when field=0, and the Accepted answer was a very simple trick, to souround the ZERO with single quotes
FROM:
SELECT * FROM table WHERE email=0
TO:
SELECT * FROM table WHERE email='0'
However, my case is slightly different in that my Query is something like:
SELECT * FROM table WHERE email=(
SELECT my_column_value FROM myTable WHERE my_column_value=0 AND user_id =15 LIMIT 1 )
Which in a sense, becomes like simply saying: SELECT * FROM table WHERE email=0, but now with a Second Query.
PLEASE NOTE: It is a MUST that I use the SECOND QUERY.
When I tried: SELECT * FROM table WHERE email='( SELECT my_column_value FROM myTable WHERE my_column_value=0 LIMIT 1 )' (Notice the Single Quotes on the second query)
MySql SCREAMED Errors near '(.
How can this be achieved
Any Suggestion is highly honored
EDIT1: For a visual perspective of the Query
See the STEN_TB here: http://snag.gy/Rq8dq.jpg
Now, the main aim is to get the sten_h where rawscore_h = 0;
The CURRENT QUERY as a whole.
SELECT sten_h
FROM sten_tb
WHERE rawscore_h = (
SELECT `for_print_stens_rowscore`
FROM `for_print_stens_tb`
WHERE `for_print_stens_student_id` =3
AND `for_print_stens_factor_name` = 'Factor H' )
The result of the Second Query can be any number including ZERO.
Any number from >=1 Works and returns a single corresponding value from sten_h. Only =0 does not Work, it returns all rows
That's the issue.
CORRECT ANSWER OR SOLUTION FOR THIS
Just in case someone ends up in this paradox, the Accepted answer has it all.
SEE STEN_TB: http://snag.gy/Rq8dq.jpg
SEE The desired Query result here: http://snag.gy/wa4yA.jpg
I believe your issue is with implicit datatype conversions. You can make those datatype conversions explicit, to gain control.
(The "trick" with wrapping a literal 0 in single quotes, that makes the literal a string literal, rather than a numeric.)
In the more general case, you can use a CAST or CONVERT function to explicitly specify a datatype conversion. You can use an expression in place of a column name, wherever you need to...
For example, to get the value returned by my_column_value to match the datatype of the email column, assuming email is character type, something like:
... email = (SELECT CONVERT(my_column_value,CHAR(255)) FROM myTable WHERE ...
or, to get the a literal integer value to be a string value:
... FROM myTable WHERE my_column_value = CONVERT(0,CHAR(30)) ...
If email and my_column_value are just indicating true or false then they should almost certainly be both BIT NOT NULL or other two-value type that your schema uses for booleans. (Your ORM may use a particular one.) Casting is frequently a hack made necessary by a poor design.
If it should be a particular user then you shouldn't use LIMIT because tables are unordered and that doesn't return a particular user. Explain in your question what your query is supposed to return including exactly what you mean by "15th".
(Having all those similar columns is bad design: rawscore_a, sten_a, rawscore_b, sten_b,... . Use a table with two columns: rawscore, sten.)
I have a query that calculates a value based on several sub-queries. The sub-queries are based on a specific timestamp. I would like to calculate this value for records for many different timestamps. I can't quite figure out how to do it.
The basic formula is (S11S4CreateSessionReqRcvd - S11S4CreateSessionRespAccSent) * 100 / S11S4CreateSessionRespAccSent
SELECT
((((select ref_data FROM test.sgw_S5S11 where timestamp = "2013-08-21 00:00:06" and ref_type = "S11S4CreateSessionReqRcvd" ) - (select ref_data FROM test.sgw_S5S11
where timestamp = "2013-08-21 00:00:06" and ref_type = "S11S4CreateSessionRespAccSent")) * 100) / (select ref_data FROM test.sgw_S5S11
where timestamp = "2013-08-21 00:00:06" and ref_type = "S11S4CreateSessionReqRcvd")) as MyCalc
I don't know how to paste in a table so here's a sample of my data in csv format
Here's a sample of my data. (I don't know how to put a table in here so it's in CSV format)
mykey,timestamp,ref_type,ref_data 1016101,"2013-08-21 00:00:06",S5S8CreateSessionReqSent,128042907 1016102,"2013-08-21 00:00:06",S5S8CreateSessionRespAccRcvd,127088838 1016103,"2013-08-21 00:00:06",S5S8CreateSessionRespRejRcvd,615553 1016104,"2013-08-21 00:00:06",S5S8CreateBearerReqRcvd,10047 1016105,"2013-08-21 00:00:06",S5S8CreateBearerRespAccSent,9932 1016106,"2013-08-21 00:00:06",S5S8CreateBearerRespRejSent,103 1016107,"2013-08-21 00:00:06",S11S4CreateSessionReqRcvd,128255390 1016108,"2013-08-21 00:00:06",S11S4CreateSessionRespAccSent,127114539 1016109,"2013-08-21 00:00:06",S11S4CreateSessionRespRejSent,713325 1016110,"2013-08-21 00:00:06",S11S4CreateBearerReqSent,10028 1016111,"2013-08-21 00:00:06",S11S4CreateBearerRespAccRcvd,9932 1016112,"2013-08-21 00:00:06",S11S4CreateBearerRespRejRcvd,42
Any help would be greatly appreciated!!!
This query is awkward because you have stored attributes on separate rows. This design is called Entity-Attribute-Value, and it's usually a bad idea for a relational database.
The following query might be a little more efficient and easier to write:
SELECT (eav.ReqRcvd - eav.AccSent) * 100 / eav.AccSent AS MyCalc
FROM (
SELECT timestamp,
MAX(IF(ref_type='S11S4CreateSessionReqRcvd', ref_data)) AS ReqRcvd,
MAX(IF(ref_type='S11S4CreateSessionRespAccSent', ref_data)) AS AccSent
FROM test.sgw_S5S11
WHERE timestamp = '2013-08-21 00:00:06'
GROUP BY timestamp
) AS eav;
PS: Use single quotes ' for string and date literals, not double quotes ". In standard SQL, double quotes are for delimiting table and column identifiers. MySQL treats the two types of quotes the same by default, but this is subject to SQL_MODE and also won't behave the same way if you ever use another brand of RDBMS. So it's a good to develop the habit of using quotes in the standard way.
Table a_table has index on string_column.
I have a query:
SELECT * FROM a_table WHERE string_column = 10;
I used EXPLAIN to find that no indexes are used.
Why? Could you help me with MySQL documentation link?
Updated: Sandbox (SQL Fiddle)
The essential point is that the index cannot be used if the database has to do a conversion on the table-side of the comparison.
Besides that, the DB always coverts Strings -> Numbers because this is the deterministic way (otherwise 1 could be converted to '01', '001' as mentioned in the comments).
So, if we compare the two cases that seem to confuse you:
-- index is used
EXPLAIN SELECT * FROM a_table WHERE int_column = '1';
The DB converts the string '1' to the number 1 and then executes the query. It finally has int on both sides so it can use the index.
-- index is NOT used. WTF?
EXPLAIN SELECT * FROM a_table WHERE str_column = 1;
Again, it converts the string to numbers. However, this time it has to convert the data stored in the table. In fact, you are performing a search like cast(str_column as int) = 1. That means, you are not searching on the indexed data anymore, the DB cannot use the index.
Please have a look at this for further details:
http://use-the-index-luke.com/sql/where-clause/obfuscation/numeric-strings
http://use-the-index-luke.com/sql/where-clause/functions/case-insensitive-search