Laravel generate subqueries with eloquent - laravel-7

I want to generate a SQL query like below:
select *
from ocs
where ocs.wfx_oc_no = 'OC11' and
ocs.id in (select id
from buyers
where buyers.buyer_code = 'B01')
Can someone help me as to how to do this with laravel subqueries?

$data = OCS::where('wfc_oc_no','OC11')->where('id', Buyers::where('buyer_code','B01')->first())->get();

Related

What is the alternative of EXISTS in cassandra?

I am trying to rewrite a SQLite query in CQL.
I know that there is no EXISTS keyword in CQL. I googled it's alternative. However, I couldn't find anything.
UPDATE users SET passsword = ? WHERE user_name = ? AND EXISTS(SELECT 1 FROM
users WHERE user_name = ? AND active_status = 1)
How can I write this query in CQL? Any help is appreciated. Thank you.
This query may be broken down to 2 queries and put an additional java logic to filter the subqueries.That will be 2 calls to database layer
Resultset rs = stmt.execute ("SELECT user_name FROM
users WHERE active_status = 1");
while ( rs.next()){
stmt.executeUpdate("UPDATE users SET passsword = ? WHERE user_name = "+rs.next());
}

MarkLogic node.js api - group by and sort by count

In a relational db you'd have something like "select name, count(1) as c from mytable group by name order by c desc". Basically I want to count how many records contain each name value and get the ones with highest counts first.
Is there a way to do a similar thing in Marklogic using the Node.js API?
Something like this should work:
var marklogic = require('marklogic');
var my = require('./my-connection.js');
var db = marklogic.createDatabaseClient(my.connInfo);
var vb = marklogic.valuesBuilder;
db.values.read(
vb.fromIndexes('name')
.withOptions({values: ['descending', 'frequency-order']});
The Querying Lexicons and Range Indexes section of the Node.js Application Developer's Guide will provide more detail.

SELECT with multiple values in DocumentDB

I have an Employees collection and I want to retrieve full documents of 10 employees whose ID's I'd like to send to my SQL SELECT. How do I do that?
To further clarify, I have 10 EmployeeId's and I want pull these employees' information from my Employees collection. I'd appreciate your help with this.
Update:
As of 5/6/2015, DocumentDB supports the IN keyword; which supports up to 100 parameters.
Example:
SELECT *
FROM Employees
WHERE Employees.id IN (
"01236", "01237", "01263", "06152", "21224",
"21225", "21226", "21227", "21505", "22903",
"14003", "14004", "14005", "14006", "14007"
)
Original Answer:
Adding on to Ryan's answer... Here's an example:
Create the following UDF:
var containsUdf = {
id: "contains",
body: function(arr, obj) {
if (arr.indexOf(obj) > -1) {
return true;
}
return false;
}
};
Use your contains UDF is a SQL query:
SELECT * FROM Employees e WHERE contains(["1","2","3","4","5"], e.id)
For documentation on creating UDFs, check out the DocumentDB SQL reference
You can also vote for implementing the "IN" keyword for "WHERE" clauses at the DocumentDB Feedback Forums.
You could also achieve this by using OR support. Below is a sample –
SELECT *
FROM Employees e
WHERE e.EmployeeId = 1 OR e.EmployeeId = 2 OR e.EmployeeId = 3
If you need more number of ORs than what DocumentDB caps, you would have to break up your queries into multiple smaller queries by employeeId values. You can also issue the queries in parallel from the client and gather all the results
The best way to do this, today would be to create a Contains() UDF that took in the array of ids to search on and use that in the WHERE clause.
Does
Select * from Employees where EmployeeId in (1,3,5,6,...)
Not work ?
thanks to ryancrawcour we know it doesn't
Another method is to use the ARRAY_CONTAINS method in the SQL API.
Here is the sample code :
SELECT *
FROM Employees
WHERE ARRAY_CONTAINS(["01236", "01237", "01263", "06152", "21224"],Employees.id).
I ran both queries ( using the IN method ) with a sample set of datasets, both are consuming the same amount of RUs.

OrmLite and Common table expressions

I'm trying to run a similar query:
sql = #"with t(id) as (select 1 )
select * from Project
where id > (select id from t)";
var projects = this.Db.Query<Project>(sql).ToArray();
For some reason the OrmLite decides to treat the sql as as "where" clause, so what ends up running is something like this:
select field1, field2 from project where with t(id) .....
Does it look for a "select" at a starting position of the query ?
Short of creating a view - is there a way to run query with CTE ?
Use the db.Sql* API's for raw SQL Queries, e.g:
var projects = db.SqlList<Project>(sql);

subsonic join to nested selected statement

Is it possible to do a join to a nested select statement in SubSonic 2.2? I am having trouble with this because I have to do a group by...
I.E.
select conn_company.company_name, SUM(t.quote_grand_total) as 'SUM' from conn_company
inner join
(SELECT *
FROM [dbo].[QuoteDBAll]
where [dbo].[QuoteDBAll].[quote_status_id] = 1
AND [dbo].[QuoteDBAll].[quote_ca_modified_date] BETWEEN '12/1/2000' AND '12/1/2009'
) t
on conn_company.company_id = t.company_id
group by company_name
having company_name = 'JACK'
Unfortunately you're not going to be able to convert that SQL into SubSonic 2 syntax. I would suggest you create a sql view based on your query and get SubSonic to generate a model from that instead.

Resources