Is it possible to write common condition in one place and reuse it (in MyBatis)? - ibatis

I have some queries inside mapper.xml which have almost the same condition. Is it possible to write the common part in one place and reuse it?
For example:
select count(*) from table1 t1 inner join table2 t2 where t1.id = t2.id;
Similarly I have another query:
select id, name from table1 t1 inner join table2 t2 where t1.id = t2.id;
I want to put table1 t1 inner join table2 t2 where t1.id = t2.id; in one place and reuse it in both queries.
Additionally I have something like:
<if test="id != 0">
AND id = #{id,jdbcType=INTEGER}
</if>
<if test="assignTo != 0">
AND assign_to = #{assignTo,jdbcType=INTEGER}
</if>
<if test="status != 0">
AND status = #{status,jdbcType=INTEGER}
</if>
and this is also common in 2 queries.

You can use SQL fragments.
For example, you can have:
<sql id="yourFragmentId1">
table1 t1 inner join table2 t2 where t1.id = t2.id;
</sql>
or
<sql id="yourFragmentId2">
<if test="id != 0">
AND id = #{id,jdbcType=INTEGER}
</if>
<if test="assignTo != 0">
AND assign_to = #{assignTo,jdbcType=INTEGER}
</if>
<if test="status != 0">
AND status = #{status,jdbcType=INTEGER}
</if>
</sql>
and then, in your queries, refer to them with <include refid="yourFragmentId1" /> and <include refid="yourFragmentId2" />, respectively.

Related

Different Filters for Different Columns

I am relatively new to SQL and I have the following question. I have the following code:
Select * from table1
LEFT JOIN table2 ON table1.name = table2.name and table1.id = table2.id
LEFT JOIN (SELECT id FROM table2 GROUP BY id) newtable ON table1.id = newtable.id
As both left joins uses data from the same table, is it possible to combine the two joins into one? How would the filters work in this case?
If your goal just to join table2 based on distinct values, then you can use WHERE and GROUP BY:
Select
*
from table1 t1
LEFT JOIN table2 t2
ON t1.name = t2.name and t1.id = t2.id
WHERE t1 id in (SELECT s2.id FROM table2 s2 GROUP BY s2.id)

T-SQL Join on columns OR fixed value

I'm trying to figure out some basic rules in T-SQL.
What I'm trying to achieve here, is to get only the records from Table1 which has a match in Table2 - AND - all records from Table1 where the 'Valid' column has a value of 1 (=true).
Previously I've done this with two selects and a UNION like this:
SELECT T1.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.ID = T2.ID
UNION
SELECT T1.*
FROM Table1 T1
WHERE T1.Valid = 1
But isn't there any other way than using multiple selects and UNION to achieve this?
While fiddling, I did the following code bit, which however only works if there's exactly one match in Table2 (otherwise it'll multiply the records by the number of matches in T2).
SELECT T1.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.ID = T2.ID
OR T1.Valid = 1
What would be the best way to achieve my goal in terms of performance?
Also please don't hold back on the comments, possible flaws, or explanations of how and why another solution might be better.
assuming that T1.ID and T2.ID is unique or a primary key:
If there are duplicates you may have to write SELECT DISTINCT T1.*. The UNION operator in the orinal selects only distinct values.
this one should do:
SELECT T1.*
FROM Table1 T1
WHERE T1.ID IN ( SELECT T2.ID FROM Table2 T2 WHERE T2.ID IS NOT NULL)
OR T1.Valid = 1
or
SELECT T1.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.ID = T2.ID
WHERE T2.ID IS NOT NULL OR T1.Valid = 1
but i think, the execution plan will be the same at the end.

Missing data from converting OR condtion in Join to Another JOIN

Few days back I asked how to change OR condtion (left join) into another left join. Here is the code below :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
OR (
t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t2.ID IS NULL
)
I converted to OR condition like below:
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)
But i am not getting records for second left join in seperate row.
With OR condition result are like :
T1.ID T2.ID
1 a
2 b
3 c
4 d
But with second left join its lile below:
T1.ID T2.ID
1 a
2 b
3 c
4rth record is from second left join. How can i bring that in left join. I dont have option for using UNION ALl as per company standards.
Please help.
Thanks
First of all please notice that you're using t2 instead of t3 in the second left join :
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2
ON t1.ID = t2.ID
LEFT JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t3.ID IS NULL
)
Anyhow, without understanding what you'r trying to do and how do you want to see your result, it's hard to answer you.
You're using left join which just gives you everything from left result set. Since, you want data from t3 which is in right, change second left join to be either right join or full join, depending on your requirements. One of the following query can give you correct result:
Editing 14-03-2015
SELECT *
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.ID = t2.ID
FULL OUTER JOIN Table2 t3
ON (
t1.col1 = t3.col1
AND t1.col2 = t3.col2
AND t1.col3 = t3.col3
AND t2.ID IS NULL
)

Opposite Of An Inner Join Query

Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);

Sql server DELETE and WITH clause

I need to build an SQL statement to delete from certain table the records that match another select statement.
In Teradata we use
delete from table1
where (col1, col2) in (
select col1,col2
from table2
)
While in SQL Server it's not allowed to have more than 1 column in the WHERE..IN clause. I thought I can use the WITH clause:
with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete from table1
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2
How to use WITH..DELETE clause? Is there another way?
This should do it:
DELETE Table1
from Table1 t1
inner join tempTable t2
on t2.Col1 = t1.Col1
and t2.Col2 = t1.Col2
First build a query that selects the rows you need:
SELECT t1.*
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.[col1] = t2.[col1] AND t1.[Col2]=t2.[Col2]
Test it to make sure it returns exactly the rows you want to delete. Then turn it into a delete statement by changing the "SELECT" to "DELETE" and removing the column list:
DELETE t1
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.[col1] = t2.[col1] AND t1.[Col
delete from table1 t1 where exists
(
select 1 from table2 t2 where t1.col1 = t2.col1 and t1.col2 > t2.col2
)
with tempTable(col1,col2) as (
select col1,col2
from table2
)
delete table1 from tempTable
where table1.col1 = tempTable.col1
and table1.col2 = tempTable.col2
This works for me
WITH CTE AS
(
SELECT TOP 50000 *
from v020101hist order by data
)
DELETE FROM CTE

Resources