MySQL syntax checking if parameter is null - mysql

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.

Related

PhpStorm - declare new type of comment in queries

Following this question, we also use another syntax in our SQL queries, which looks like that :
SELECT
id
FROM
myTable
WHERE
first_criteria = true
//withCondition//AND second_criteria = false
The //withCondition// syntax allows us to update the query by activating or disabling said line.
If possible, I'd want to be able to tell PhpStorm this specific syntax in SQL queries is a comment, so it won't display an error anymore.

CI active record style sql queries

I am new in Code Igniter and like its active record feature now is there any useful steps or tips or any guidness how do i convert my pervoiusly written simple SQL Queries in CI style like this is my perviouly written simple query
SELECT *
FROM hs_albums
WHERE id NOT IN (SELECT album_id
FROM hs_delete_albums
WHERE user_id = 72
AND del_type = 1)
AND ( created = 72
OR club_id IN (SELECT cbs.id
FROM hs_clubs cbs
INNER JOIN hs_club_permissions cbp
ON cbs.id = cbp.club_id
WHERE cbp.user_id = 72
AND cbp.status = 2)
OR group_id IN (SELECT gps.id
FROM hs_groups gps
INNER JOIN hs_group_permissions grp
ON gps.id = grp.group_id
WHERE grp.user_id = 72
AND grp.status = 2)
OR comp_id IN (SELECT cmp.id
FROM hs_companies cmp
INNER JOIN hs_comp_permissions comp
ON cmp.id = comp.comp_id
WHERE comp.user_id = 72
AND comp.status = 2) )
The short answer is: You don't.
CodeIgniter's Active Record implementation is basically a layer on top of SQL that makes writing queries easier by:
Automatically escaping values
Automatically generating the appropriate query syntax for the database, so that the application can be more easily ported between databases (for instance, if you didn't use Active Record to write a query, and then wanted to move from MySQL to PostgreSQL, then you might well need to rewrite the query to make it work with PostgreSQL)
Providing a syntax for queries in PHP directly, thus avoiding the context switching between PHP and SQL.
However, it can't do everything SQL can do, and while I would always try to use ActiveRecord where possible, there comes a point where you're better off forgetting about using it and just using $this->db->query() to write your query directly. In this case, as mamdouh alramadan has said, CodeIgniter doesn't support subqueries so you can't replicate this query using ActiveRecord anyway.
The thing to remember is that ActiveRecord is a tool, not a requirement. If you're using CodeIgniter and aren't using an ORM instead, you should use it for the reasons mentioned above. However, once it starts getting in the way, you should consider whether it would be better practice to write your query manually instead.

MySQL "in" command

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'))
);

Change order of MySQL query keywords?

I'm using this 3rd party report generating software. It has the following steps:
1) insert your SQL statement into a webpage.
2) invoke an API to send the a set of primary keys to the Query
3) A Report is generated.
Unfortunately, the software is dumb, and simply appends the WHERE clause after the SQL statement. However with MySQL the WHERE statement is supposed to be before the GROUP BY. So when the API appends a WHERE it fails because its invalid SQL. Is there some way to tell MySQL to expect the WHERE statement at the end?
select incident.incidentID,
GROUP_CONCAT(moccode2.Description) as MOC2Description
from incident
join incidentmoc on incident.IncidentID = incidentmoc.IncidentID
inner join moccode2 on moccode2.id = incidentmoc.moccodeid
/* WHERE should go here */
group by incident.incidentID
/* I want the WHERE to go here */
Derek Kromm is basically correct in what I asked for, unfortunately I have additional constraints. It's still going to append the WHERE.
So I tried this:
select incident.incidentID,
GROUP_CONCAT(moccode2.Description) as MOC2Description
from incident
join incidentmoc on incident.IncidentID = incidentmoc.IncidentID
inner join moccode2 on moccode2.id = incidentmoc.moccodeid
group by incident.incidentID
HAVING incident.IncidentID > 1
////////////////////////////////////////
now software appends WHERE invalid SQL
Use the HAVING keyword
This link has some details around using it: http://www.mysqltutorial.org/mysql-having.aspx

mysql to codeigniter active record help

Active record is a neet concept but sometimes I find it difficult to get more complicated queries to work. I find this is at least one place the CI docs are lacking.
Anyway,
This is the sql I wrote. It returns the expected results of quests not yet completed by the user that are unlocked and within the users level requirements:
SELECT writing_quests . *
FROM `writing_quests`
LEFT OUTER JOIN members_quests_completed ON members_quests_completed.quest_id = writing_quests.id
LEFT OUTER JOIN members ON members.id = $user_id
WHERE writing_quests.unlocked =1
AND writing_quests.level_required <= $userlevel
AND members_quests_completed.user_id IS NULL
This is the codeigniter active record query, it returns all quests that are unlocked and within the users level requirement:
$this->db->select('writing_quests.*');
$this->db->from('writing_quests');
$this->db->join('members_quests_completed', 'members_quests_completed.quest_id = writing_quests.id', 'left outer');
$this->db->join('members', "members.id = $user_id", 'left outer');
$this->db->where('writing_quests.unlock', 1);
$this->db->where('writing_quests.level_required <=', $userlevel);
$this->db->where('members_quests_completed.user_id is null', null, true);
I'm guessing there is something wrong with the way I am asking for Nulls. To be thorough, I figured I'd include everything.
I agree that sometimes CI active record can overcomplicate things. Try this for your IS NULL where clause:
$this->db->where('members_quests_completed.user_id IS ','NULL',false);
Try also to enable the profiler or echo the generated query with:
echo $this->db->last_query();
That might shed some light on what the issue is.
Sometimes CI makes it a bit complicated... you can always use $this->db->query("your very long query") to simplify a bit (if I recall correctly strings are still escaped - not sure). It's a personal opinion.
$this->db->where('column IS NOT NULL')
in your case
$this->db->where('members_quests_completed.user_id is not null');