MySQL "in" command - mysql

I want to know how we can do this using the IN comparison syntax.
The current SQL query is :
select * from employee
where (employeeName = 'AJAY' and month(empMonth) = month(curdate()))
or (employeeName ='VINAY' and month(empMonth) = month(curdate()))
I tried it using IN comparison function, but am unable to properly set the pieces. Can any one help me?
select * from employee
where employeeName in ('AJAY','VINAY')
and month(empMonth) = month(curdate()); // ERROR.
I tried it in MySQL Query.
Thank You,
Sindhu

Your solution is fine for most DBMS (data-base management systems). As far as I know it is no problem in MySQL. But some years ago I had similar problems in DB2 and also in another more exotic DBMS named "Focus".
Maybe this can help:
Put the complete where-block into a pair of brackets.
Inside this block put each comparison in a pair of brackets again.
Move the IN-Comparison to the end of the where-block.
This would transform your example into this code:
SELECT *
FROM employee
WHERE (
(month(empMonth) = month(curdate())
AND
(employeeName IN ('AJAY','VINAY'))
);

Related

Using concat to put together a simple string, a subquery, and another simple string

I am needing to use concat to create custom sql statements.
I'm trying to use
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1), 'nam.jpg')
The goal is to update the image_loc in setuptable to "/file/to/image/UIDnam.jpg"
I'm getting Syntax errors and "Expected End of Statement".
How can i tie a simple string, a subquery, and another simple string together to update a field?
I found a way to make it work. If any one has any tips of improvement, I'm up to learn!
*I can't use variables
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1)+, 'nam.jpg')
I found slightly better following structure :
update setuptable
set image_loc = CONCAT('/file/to/image/', t1.UID, 'nam.jpg')
from table1 t1
I don't have mysql server at hand for the moment, so I have not tested.

Filtering SQL results by date

Here is my query for a maintenance dates list.
SELECT `checkdates`.`checkdateplanneddate`, `checkdates`.`checkdatevehicle`, `checktypes`.`checktype`, `checktypes`.`emailto`, `checktypes`.`daysnotice`
FROM `checkdates`
, `checktypes`
WHERE `checktypes`.`checktype` = `checkdates`.`checkdatechecktype`;
The idea is..
Everyday the server will email customers to let them know which checkdates are coming, based on the days notice that is set for that type of check. (see image)
Currently it is showing all checkdates.
All i need to do is filter the list so it only shows the dates that are
"Todays date plus checktypes.daysnotice"
I have tried many different queries, but cannot seem to get the right combo.
Thank you in advance
I have attached an image to show that the data is available
If I understand your question correctly, and assuming that you are running MySQL (as the use of backticks for quoting and the phpmyadmin screen copy indicate), you can use date arithmetics as follows:
SELECT cd.checkdateplanneddate, cd.checkdatevehicle, ct.checktype, ct.emailto, ct.daysnotice
FROM checkdates cd
INNER JOIN checktypes ct ON ct.checktype = cd.checkdatechecktype
WHERE cd.checkdateplanneddate = current_date + interval ct.daysnotice day
The where condition implements the desired logic.
Side notes:
Use standard, explicit joins! Implicit joins (with commas in the from clause) is a very old syntax, that should not be used in new code
Table aliases make the query easier to write and read

MySql Query for selecting users according to the Date of Today

$user_countday = $wpdb->get_var( "SELECT * FROM {$wpdb->prefix}users WHERE (DATE('user_registered') = DATE(NOW())" );
print_r($user_countday);
I am using the above Query for selecting all registerd users of today in wordpress but this is not displaying any answer.
For trying many hours, I didn't got any solution.
Where is the problem in my code?
I am not very familiar with WordPress, but an educated guess is that you are using the wrong function to load results. You are using get_var while you may need to use get_results.
1 more parentheses is needed from dbms query should returned row if wordpress is accurate
( "SELECT * FROM {$wpdb->prefix}users WHERE DATE(user_registered) = getdate())" )

MySQL syntax checking if parameter is null

I am looking for the way to execute MySQL statement checking if given parameter exists. As I remember I can do the following in Oracle to achieve that:
select s.* from Site s
where s.study = :study
and (:enabled is null or s.enabled = :enabled)
is anything like that possible in MySQL too? The same code executes without error but never return any records.
My goal here is to avoid multiple lfs and elses in my java code. It should work the way that the query looks like that when enabled parameter is null:
select s.* from Site s
where s.study = :study
and like that if the parameter is not null:
select s.* from Site s
where s.study = :study
and s.enabled = :enabled
and I want to do that with a single query
I believe this is what you are asking:
SELECT s.* from Site s
WHERE s.study = "some_study"
AND (s.enabled IS NULL OR s.enabled = '' OR s.enabled = "enabled");
Unfortunately it is highly dependent on database driver. My initial query works when run in database tools but doesn't have to when it comes to run it by JPA. So I'm to close this question as it doesn't require further answers. I'm sorry lads for wasting your time.

Find recent object changes in SQL Server Database

I've added and modified several (new and existing resp.) tables and stored procs, for a particular database and server, in last 3 months.
I was thinking if there's any SQL query by which I can determine all those changes.
Thanks.
Query the sys.objects table to find the objects that changed and filter by modify_date and type; U = User table, P = Stored procedure.
select *
from sys.objects
where (type = 'U' or type = 'P')
and modify_date > dateadd(m, -3, getdate())
This approach will tell you what objects have changed, but not the specific changes.
Hi you can get the changed/modified db object details with this query
select name,create_date,modify_date
from sys.procedures
order by modify_date desc
Thanks
I don't think you're going to be able to find what you're looking for. SQL Server just doesn't track that information out of the box.
To handle this in the future, you can use some kind of source control (redgate, for one: http://www.red-gate.com/products/sql-development/sql-source-control),
or you can set up a DDL trigger (one such technique is described here: https://dba.stackexchange.com/questions/33541/how-to-keep-history-of-sql-server-stored-procedure-revisions/33544#33544).