I have written a mysql query
select * from user where registration_date="2016-05-20"
But it returns wrong value. Please help me
Use
select * from user where date(registration_date)=date("2016-05-20")
Is there a way to select data from any of the existing columns in a databse?
Something like SELECT * FROM mytable WHERE AnyOfTheTableColumns = Something
No, column name can't be variable! You must define your query with explicit definition of columns as;
SELECT * FROM mytable
WHERE column1=Something OR column2=Something OR column3=Something
I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()
A bit of background
I'm running mySQL on a mac
I have a few databases setup that have been working okay
I have recently created a new table from a sqlDump from another server.
I am having an issue with new rows that equal = a value that I know exists
e.g. Table setup
id=1 name='dave' - already exists in database
id=2 name='john' - I add a new row
Following are the sql I tried with results...
Select * from tablename where id=1 -- I get the correct Dave result.
Select * from tablename where `name` = 'dave' -- I get the correct Dave result.
Select * from tablename where id=2 -- I get the correct John result.
Select * from tablename where `name` like 'joh%' -- I get the correct John result.
Select * from tablename where `name` = 'john' -- (No result!) eek!
Anyone seen this before? it's in the database but a direct match on the name field is not yielding a result.
Thanks in advance
M
One possibility: there could be a trailing space after 'john' in the name column.
One way to check that:
select `name`,char_length(`name`), char_length('john')
from tablename
where id = 2
An easy way to not have to deal with that problem would be to trim your input (if you don't expect to ever have preceding or trailing white space.
In that case you could have a query like:
Select * from tablename where trim(`name`) = trim('john')
I agree with the comments on your question, that it is most likely a hidden space or something similar. If you include the column definitions so we can check the data that your using with the types we could help more. Remove the entry and and retry with a different name other than john and see if you can replicate the problem.
I was wondering if something like this is possible in MySQL:
SELECT * FROM tmpTable WHERE id = 1 AND ISTRUE(UPDATE tmptTable SET value = 1)
Or
SELECT * FROM tmpTable WHERE id = (INSERT INTO tmptTable (name) VALUES ('test'))
Or the same query with UNION or something
What i'm trying to do with this is SELECTing only the records that are updated/by the ID's they are inserted. I want to know if it's acutally possible to do an Update or Insert query INSIDE an other query.
Looks like you are trying to do composable DML. No this isn't supported in MySQL. – Martin
https://stackoverflow.com/users/513811/martin
He just gave the answer, but as a comment...