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();
Related
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');
I am facing an issue with the query in postgreSql. Below is the query.
UPDATE t_e20so1_fieldrulethen AS fthen
SET c_thenfieldid = t1.c_fieldschemaid
FROM t_sys_fieldschema AS t1
WHERE fthen.c_lyrathenfieldid = t1.c_lyraid
AND fthen.c_rulefor = 5
AND t1.c_fieldtype = 18
AND t1.c_tablegroupsid IN (
CASE
WHEN fthen.c_iffieldid = t1.c_id THEN (SELECT
c_targettableid
FROM t_sys_tablegroups
WHERE c_parentid = 'c0b2f85c-bc93-466b-a54d-b1330440db98')
ELSE (SELECT c_targettableid
FROM t_sys_tablegroups
WHERE c_parentid =
'c0b2f85c-bc93-466b-a54d-b1330440db98')
END );
As per above query, i am updating t_e20so1_fieldrulethen table from t_sys_fieldschema. One of the conditions to check is t_sys_fieldschema.c_tablegroupsid should be having specific values which are in and I am fetching them from table t_sys_tablegroups.
Above query gives me error as shown below:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Here, if I remove case from the query (below is what I mean) it works properly.
UPDATE t_e20so1_fieldrulethen AS fthen
SET c_thenfieldid = t1.c_fieldschemaid
FROM t_sys_fieldschema AS t1
WHERE fthen.c_lyrathenfieldid = t1.c_lyraid
AND fthen.c_rulefor = 5
AND t1.c_fieldtype = 18
AND t1.c_tablegroupsid IN (SELECT c_targettableid
FROM t_sys_tablegroups
WHERE
c_parentid = 'c0b2f85c-bc93-466b-a54d-b1330440db98')
Now I have only one select query in the "IN" clause.
I've checked that kind of case statement(two nested selects) - and it cannot be done that way. You generate two separate lists in one CASE.
One list for all records where fthen.c_iffieldid = t1.c_id and the other for ELSE statement.
As I wrote many times never use nested selects in "IN" clause. It is killing performance and causing many problems. Use "EXISTS".
As your CASE seems to be redundant (both WHEN and ELSE returns same value) change it that way and it will be faster.
UPDATE t_e20so1_fieldrulethen AS fthen
SET c_thenfieldid = t1.c_fieldschemaid
FROM t_sys_fieldschema AS t1
WHERE fthen.c_lyrathenfieldid = t1.c_lyraid
AND fthen.c_rulefor = 5
AND t1.c_fieldtype = 18
AND exists (select from t_sys_tablegroups t2
where t1.c_tablegroupsid=t2.c_targettableid
and c_parentid = 'c0b2f85c-bc93-466b-a54d-b1330440db98');
I am trying to query a column which contains string such as
595.1,N30.10
630.5,E10
I have tried separating the two values into different columns
split_part(code, ',', 1) AS code1,
split_part(code, ',', 2) AS code2
But now I see that some of the rows have 3 (or could be more)
785.59, R57.1, R
I wonder if there is a way to specify and query only the first part of the string without having to split the string. In this case only look for enteries with 595.1,785.59 and ignore the rest.
SELECT distinct ON (id) id,time,year,code
FROM data
where code= ANY('{595.1,785.59}');
I think you already the have logic you need, you only need to thread it together:
SELECT DISTINCT ON (id) id, time, year, code
FROM data
WHERE split_part(code, ',', 1) = ANY('{595.1,785.59}');
This logic appears to be working in the demo below.
Demo
To query on first part of code column you can do like below
SELECT * FROM tableName
WHERE split_part(code, ',', 1) = somevalue
I'm developing an extension for TYPO3 CMS 8.7.8. I'm using query->statement() to select all fields from a single table, plus 1 field from another table. I get a QueryResult with the proper models and I would like to have that 1 extra field added to them. Is that possible?
You can do SQL queries with the ->statement(...) method and in that, use normal JOIN commands
From the documentation
$result = $query->statement('SELECT * FROM tx_sjroffers_domain_model_offer
WHERE title LIKE ? AND organization IN ?', array('%climbing%', array(33,47)));
So you can do JOINs on whatever table you want to (also code from the documentation)
LEFT JOIN tx_blogexample_person
ON tx_blogexample_post.author = tx_blogexample_person.uid
But you will end up with the raw data from the mysql query. If you want to transform it into a object, use the Property Mapper
You can use JOIN in your sql statments like below.
$query = $this->createQuery();
$sql = 'SELECT single.*,another.field_anme AS fields_name
FROM
tx_single_table_name single
JOIN
tx_another_table_name another
ON
single.fields = another.uid
WHERE
O.deleted = 0
AND O.hidden=0
AND O.uid=' . $orderId;
return $query->statement($sql)->execute();
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`