MySQL Selecting where datetime column = - mysql

Hi I am trying to get the following sql to work in mysql but it always return an empty result set - however there are definitely entries that match the criteria.
I'm new to mySQl so would appreciate if someone could point out where I am going wrong.
SELECT * FROM `ch_results`
WHERE 'readingDateTime' = '2011-03-29 20:00:00'

Remove the quotes around the field name:
SELECT *
FROM `ch_results`
WHERE readingDateTime = '2011-03-29 20:00:00'
Your current query compares string 'readingDateTime' to another string, '2011-03-29 20:00:00', which comparison of course never holds true.

Drop the quotes on the 'readingDateTime'. This is comparing strings to each other.
WHERE readingDateTime = '2011-03-29 20:00:00'

Related

MySQL In clause not giving the right result

In a MySQL table i have a field, containing this value for a given record : "1908,2315,2316"
Here is my sql Query :
SELECT * FROM mytable WHERE 2316 IN (myfield)
I got 0 results!
I tried this :
SELECT * FROM mytable WHERE 2315 IN (myfield)
Still 0 results
And then i tried this :
SELECT * FROM mytable WHERE 1908 IN (myfield)
Surprisingly i obtained the record when searching with 1908! What should i do to also obtain the record when searching with 2315 and 2316 ? What am i missing ?
Thanks
You appear to be storing comma delimited values in a field. This is bad, bad, bad. You should be using a junction table, with one row per value.
But, sometimes you are stuck with data in a particular structure. If so, MySQL provides the find_in_set() functions.
SELECT *
FROM mytable
WHERE find_in_set(2316, myfield) > 0;
You can't use IN() over comma separated list of no.s its better to normalize your structure first for now you can use find_in_set to find results matching with comma separated string
SELECT * FROM mytable WHERE find_in_set('1908',myfield) > 0
This question has been asked and answered before, but I don't want to hunt for it; this question should be closed as a duplicate. But, to answer your question:
The commas in the string, the column value, are just characters. Those are part of the string. They aren't seen as "separators" between values in the SQL text. The way SQL sees it, the column contains a single value, not a "list" of values.
So, in your query, the IN (field) is equivalent to an equals comparison. It's equivalent to comparing to a string. For example:
... WHERE 2316 = '1908,2315,2316'
And those aren't equal, so the row isn't returned. The "surprisingly" finding of a match, in the case of:
... WHERE 1908 IN ('1908,2315,2316')
that's explained because that string is being evaluated in a numeric context. That is, the comparison returns true, because all of these also true:
... WHERE 1908 = '1908,2315,2316' + 0
... WHERE 1908 = '1908xyz' + 0
... WHERE 1908 = '1907qrs' + 1
(When evaluated in a numeric context, a string gets converted to numeric. It just happens that the string evaluates to a numeric value that equals the integer value it's being comparing to.)
You may be able to make use of the MySQL FIND_IN_SET function. For example:
... WHERE FIND_IN_SET(2316,'1908,2315,2316')
But, please seriously reconsider the design of storing comma separated list. I recommend Bill Karwin's "SQL Antipatterns" book...
http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
In mysql IN clause is utilized as
SELECT * FROM mytable WHERE column_name IN (set_of_values) ;
Mention column name instead of values
Please try
SELECT * FROM mytable WHERE LOCATE(CONCAT (',', 2316 ','), CONCAT (',',myfield,',' ) ) <>0

MySQL returns all rows when field=0 from SECOND Select query

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.)

MySql: Calculate a value based on multiple sub-queries

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.

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).

update a column by subtracting a value

I'm trying to come up with a MySQL query that will update points... Can I do something like this?
UPDATE `a75ting`.`username` SET `points` = '`points` - 5'
UPDATE a75ting.username
SET points = points - 5
by putting the single quotes around the "points -5", you converted that expression into a plaintext string. Leaving it without the quotes lets MySQL see you're referring to a field (points) and subtracting 5 from its current value.
Run this query to find out the difference:
SELECT '`points` - 5' AS string, `points` - 5 AS expression
FROM a75ting.username