MyBatis select query with IN but without forEach - mybatis

how can I use a select query where I have only two values for one column without using for each loop ?

Without using IN, you can use OR:
SELECT * FROM `table` WHERE co1 = `value1` OR col1 = `value2`

Related

Use postgresql query results to form another query

I am trying to select from one table using the select result from another table. I can run this in two queries but would like to optimize it into just one.
First query.. Select ids where matching other id
select id from lookuptable where paid = '547'
This results in something like this
6316352
6316353
6318409
6318410
6320468
6320469
6320470
6322526
6322527
6324586
6324587
6326648
I would like to then use this result to make another selection. I can do it manually like below. Note, there could be many rows with these values so I've been using a IN statement
select * from "othertable" where id in (6316352,6316353,6318409,6318410,6320468,6320469,6320470,6322526,6322527,6324586,6324587,6326648);
select
ot.*
from
"othertable" as ot
join
lookuptable as lt
on
ot.id = lt.id
where
lt.paid = '547'
The IN operator supports not just value lists but also subqueries, so you can literally write
select * from "othertable" where id in (select id from lookuptable where paid = '547');

Compare 2 fields in where statement ORMLite

Is it possible to compare 2 fields from the table in where condition in ORMLite without using where().raw(statement, args)?
SQL query looks like
select
*
from
table
where
table.f1 = table.f2
try This ... i hope Its Work For You !!!
select (case when (table.f1 = table.f2 ) then 'EQUAL' else 'NOT_EQUAL' end) as one from table
OR
SELECT * FROM table WHERE f1 LIKE 'f2';
OR
SELECT * FROM table WHERE f1 = f2;
Don't know why I missed this last time. ORM Lite doc contains the answer:
ColumnArg object is designed to compare one column against another.
QueryBuilder<Table, String> queryBuilder = tableDao.queryBuilder();
queryBuilder.where().eq(Table.f1,
new ColumnArg(Table.f2));
List<Table> results = queryBuilder.query();

How to create a filter that does the SQL equivalent of WHERE ... IN for SQLite.Swift

I would like to filter results for the same column with multiple values
example in sql:
SELECT * FROM myTable WHERE status = 1 AND current_condition = ("New", "Working")
this will return all rows from myTable where the status is 1 and the current_condition is "New" OR "Working"
how do I do this in SQLite.swift?
You can use raw SQL in Swift. So you can use the string you posted.
Raw SQL
Using Filters
I use Filters, gives me more insight.

how to select from one table where no matching row in another (but has multiple)

I have tried this:
SELECT *
FROM svc00100
WHERE NOT EXISTS (SELECT *
FROM svc00101
WHERE TECHSTAT = 'INA'
AND svc00100.TECHID = svc00101.TECHID)
I want to select rows from svc00100 but not if there is a row in svc00101 with the same TECHID and with TECHSTAT = 'INA'. But, there are multiple rows in svc00101 with the TECHID matching, some having 'INA' and some having other stuff. I want to eliminate/ignore any TECHID where there is any row in svc00101 with TECHID and 'INA' for techstat. Using SQL server BTW if that helps.
You can use left outer join and Where clause. Like this:
select svc00100.* from svc00100
left outer join svc00101 on TECHSTAT = "INA"
and svc00100.TECHID = svc00101.TECHID
where svc00101.KEY is null
Instead of KEY you should pass name of NOT NULL column. For example Primary Key.

TSQL find records with different writing in another table

I have a "MasterTable" with the following records:
MasterTable:
Col1
PX02894
PX02895
PX02896
PX02897/98
From the lookup table I want to get the Col2 links, keeping the formatting of the MasterTable, represented as the Output table below.
LookupTable:
Col1 Col2
PX02894-PX02895 Link001
PX02896 Link002
PX02897-PX02898 Link003
OutputTable:
Col1 Col2
PX02894 Link001
PX02895 Link001
PX02896 Link002
PX02897/98 Link003
As you can see the writing is different "/" and "-".
I've tried with
len(col1) > 7 THEN LEFT(col1,5) + RIGHT(col1,2)
but that's wrong. Do I need a Union first?
Here's a Fiddle
What do I need to do here? Thanks in advance.
select m.col1,l.col2
From MasterTable m
inner join linkTable l
On (Substring(m.col1,1,7) = SubString(l.col1,1,7)) or (Substring(m.col1,1,7) = Substring(l.col1,9,7))
should do it as long as you can trust the data formating. if not a few more checks e.g
Substring(l.col1,8,1) = '-'
Try this --
This works in sybase-- if you using SQL server find alternate functions
select substring(col1,1,(charindex("-",col1)-1)),
UNION
select substring(col1,(charindex("-",col1)+1),char_length(col1))
Thanks,
Gopal