Where Equals Subquery Laravel - sql

How can I convert the following SQL query to Laravel:
SELECT *
FROM SomeTable
WHERE some_column =
(
SELECT some_column
FROM SomeTable st1
where st1.some_column2 = SomeTable.some_column2
)
This query is just an example query. I know that Laravel has a whereIn function for subqueries. I am looking for something similar about equality of a column with a subquery.

DB::table('SomeTable')
->select('*')
->where('some_column','=',function($query) {
$query->from('SomeTable')
->select('some_column')
->where('SomeTable.some_column2','=',DB::raw('SomeTable.some_column2'));
})
->get();

Related

how to use sql query in Laravel query

SELECT
COUNT(*)
FROM
(
SELECT
tbl_order.order_id
FROM
tbl_order
where delete_status=1
group by patient_id,event_no
) AS DerivedTableAlias
I want this sql in laravel
Try out this query builder query:
/DB::table('tbl_order')
->select('patient_id','event_no')
->selectRaw('COUNT(tbl_order.order_id) AS count')
->where('delete_status', 1)
->groupBy('patient_id','event_no')
->get();

build a dynamic string in sql Equal statement

I need something to build a string in an sql equal statement, composed by two part, the first one choosed by me and the second one dynamically generated by a query
select * from table
where param1= 'test' + (select distinct param2 from table2 where ...)
the second select always return only one record of course.
So the where clause should be where param1='test'+param2
I tried concat function but it doesn't seem the correct way; any suggestion?
Oracle uses the ANSI standard || operator for string concatenation. So, use this:
SELECT *
FROM yourTable
WHERE param1 = 'test' || (SELECT DISTINCT param2 FROM table2 WHERE ...);
The CONCAT function should also work here:
SELECT *
FROM yourTable
WHERE param1 = CONCAT('test', (SELECT DISTINCT param2 FROM table2 WHERE ...));
select * from table
where param1= 'test' || (select distinct param2 from table2 where ...)

simple subquery not working DB2

hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)

Simplify PostgreSQL function

I have a PostgreSQL stored procedure that contains the following code:
IF something = TRUE THEN
SELECT id INTO some_id FROM some_table WHERE some conditions LIMIT 1;
RETURN QUERY SELECT * FROM some_table WHERE some conditions LIMIT 1;
ELSE
SELECT id INTO some_id FROM some_table WHERE some OTHER conditions LIMIT 1;
RETURN QUERY SELECT * FROM some_table WHERE some OTHER conditions LIMIT 1;
END IF;
DELETE FROM some_table where id = some_id;
Is there a way to simplify the above code? I guess there is nothing we can do
about the repeated code in the IF and in the ELSE, but is there a way to avoid
having 2 SELECT's every time? Is it possible to insert something in some_id while
RETURN QUERY?
If the function does only what you posted then it is not necessary:
delete from some_table
where
something and (some conditions)
or
something is not true and (some other conditions)
returning *

jpql INTERSECT without INTERSECT

i have four queries that return intergers.
select listOfIntegers from [something]...
(edit: the results are ROWS)
and need a way to do
select ...
intersect
select ...
intersect
select ...
intersect
select ...
but in jpql there is no intersect as such.
so, is there a way to mimic the behavior using some other jpql to get the same result?
(for those that are unsure about intersect) basically i need to get all the values that appear in ALL the selects...
result from select 1: 1,2,3,4
result from select 2: 1,2,5,6
result from select 3: 1,2,7,8
result from select 4: 1,2,9,0
so the result i want with intersect: 1,2
thnx a lot
p.s. there is no chance to use ANYHTING OTHER THAN JPQL :( no native queries, etc...
Can you use something like this?:
select s1.result
from select_1 as s1
where exists (
select *
from select_2 as s2
where s2.result = s1.result
)
and exists (
select *
from select_3 as s3
where s3.result = s1.result
)
and exists (
select *
from select_4 as s4
where s4.result = s1.result
);