Pass Dynamic Parameter in Select query in mysql - mysql

Can you please help me on the below query?
I have below select query, in which i want to pass a parameter dynamically to the hostname column. Is there way to achieve it?
select *
from All_HOSTS
where hostname='example.com'
order by FROM_UNIXTIME(start_epoch);
In ORACLE we have like this
select * from all_hosts where hostname=&1;
so it will ask parameter dynamically when I execute the select query - Is there way in MYSQL as well?
Please assist.

Related

mysql datetime field matching query not working correctly

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

Trunc date field in mysql like Oracle

I am to not able to use the 'trunc(in oracle)' function in 'mysql' database. I have a table called dlb_cc_purchase and date field called due_date in my 'mysql' database. The data displaying in the date field like 20-11-2014 00:00:00 (20-nov-2014). in oracle we are using query
select * from dlbcc_purchase where trunc(due_date) = '20-nov-2014'
Oracle DB will fetch the row with due date 20-11-2014 00:00:00. How can I use this function in 'mysql'?
I know this is a basic question, but i was trying to do this for long time with truncate, str_to_date... but not able to fetch value. Please help.
Use DATE(expr) function. Query example:
SELECT *
FROM dlbcc_purchase
WHERE DATE(due_date) = '2014-11-20'
You can use DATE_FORMAT().
example:
select * from dlbcc_purchase where DATE_FORMAT(due_date,'%d-%b-%Y') = '20-nov-2014'

MySQL SELECT statement returns no record when I am searching with a value that contains hyphen(-)

Problematic Query:
SELECT * FROM my_links WHERE taxonomy='ait-dir-item-category'
Above query returns no record and no error.
I am getting empty result, but I have exactly same value 'ait-dir-item-category' for taxonomy column.
But following query returns result
Perfect Query:
SELECT * FROM my_links WHERE taxonomy='post_tag'
Please tell me what is the problem.
Try with LIKE statment
SELECT * FROM my_links WHERE taxonomy LIKE '%ait-dir-item-category%'
Did you tried using like statement
Can you try like this,
select * from my_link where taxonomy like '%ait-dir-item%'

How to get minimum value from a set of values returned by SELECT query?

I have written a SELECT query, which will return a set of values, for ex.,
The following one is the actual table -
select data from tab1 where id <5; // This statement returns me the following table
I am trying to get the minimum value of the resultant table. I have tried the following query for that -
select MIN(select data from tab1 where id<5);
SQLite Browser says, there is an error in the select statement. My doubt is, whether we can give a select statement directly into an aggregate function like MIN()? If not, can you please suggest me a better way to do this task?
Thanks in advance.
Try this way:
select MIN(data)
from tab1 where id<5;

How to loop through the resultset of query in the sql server user defined function?

I want to apply loop in the resultset of query in the user defined function in sql server.
Here is the query:
select acc_no, balance from sav_acc_mcg where scheme_id ='001'
This query gives a no. of rows of acc_no. Now what i need to do is loop through that result and do some work with the result. But the problem is to loop through the resultset of the query. So help me out. Thanks in advance.
select sum(balance) as Total from sav_acc_mcg where scheme_id ='001'
Sounds like you need CROSS APPLY. See #Quasnoi's blog (search "TVF"): http://explainextended.com/2009/07/16/inner-join-vs-cross-apply/