How to fetch data from multiple tables using where class in NotORM?
CODE
SELECT tbl1.description,
concat(tbl2.first_name, ' ', + tbl2.last_name) name,
count(tbl3.description)
FROM table1 tbl1, table3 tbl3,table2 tbl2
WHERE tbl1.id = tbl3.s_id
and tbl1.value= tbl2.value
group by tbl1.description,name
i think, simply you can try like this
$db->table1()->select("description,(SELECT first_name FROM table2 where table2.value=table1.value)as firstname")->group("description")->fetch();
NotORM is designed to work with well-named tables and columns. The default naming convention would lead to this kind of request (if I guess your need right):
SELECT table1.description,
CONCAT(table2.first_name, ' ', + table2.last_name) name,
COUNT(table3.description)
FROM table1, table2, table3
WHERE table1.id = table3.table1_id
AND table2.id = table1.table2_id
GROUP BY table1.description, name
In notOrm
$db->table1()
->select("table3.description")
->select("CONCAT(table2.first_name, ' ', + table2.last_name) AS name")
->select("COUNT(table3.description) AS c")
->group("table1.description, name");
There is no need to join these tables together. It's NotOrm's job.
Related
Im trying to use GORM to select only items from the parent table that have a matching condition in a related table.
type Table1 struct {
gorm.Model
Name string
Email string
Items Table2
}
type Table2 struct {
gorm.Model
Product string
otherfield string
}
I want to return all Table1 items that have Product in Table2 set to a specific value. So far I am getting mssql: The multi-part identifier "visits.sign_out_time" could not be bound. a lot.
My command is
var items []Table2
db.Debug().Preload("Table2").Where("table2.product = ?", "xxx").Find(&items).GetErrors()
Not entirely sure where I'm going wrong but for whatever reason the .Where() cannot access the second, preloaded table. How do I go about using GORM to achieve what I am trying to do?
Thanks,
Alex
The Where("table2.product = ?", "xxx") cannot access the second (preloaded) table because Preload is not a JOINS, it's a separate SELECT query. Your code creates two separate queries, something like this:
// first query
SELECT * FROM table1 WHERE table2.product = 'xxx';
// second query
SELECT * FROM table2;
In order to return all Table1 records that have Product in Table2 set to a specific value you have to do the following:
var t1 []Table1
err = db.
Where(`EXISTS(SELECT 1 FROM table2 t2 WHERE t2.product = ? AND table1.id = t2.table1_id)`, productValue).
Find(&t1).Error
Note that AND table1.id = t2.table1_id part is just an example how the two tables might be related, you may have a different relation and you'll need to modify the query accordingly.
If you want GORM to populate the t1.Items with the Table2 data, you prepend Preload("Items") to the above query.
If you only want only Items, you can directly query on Table2 no need to preload Table1
var items []Table2
db.Where("product = ?", "xxx").Find(&items).GetErrors()
Or you need all data of Table1 then Join with table2 then use where clause
db.Debug().Joins("JOIN table2 ON table1.id = table2.table1_id")
.Where("table2.product = ?", "xxxx").Find(&table1data)
Here I don't see any foreign key in Table2 for join.You can add one.
type Table2 struct {
gorm.Model
Product string
Table1ID uint
}
Is it possible to rewrite the following query
SELECT CT.GROUP, CT.EMP_ID, HT.EFF_DT
FROM CURR_TABLE CT
JOIN (SELECT GROUP, EMP_ID, MAX(EFF_DT) AS EFF_DT
FROM HIST_TABLE
WHERE STAT = 'A'
GROUP BY GROUP, EMP_ID) HT ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID
WHERE CT.GROUP = :1
AND CT.EMP_ID = :2
in a way that is similar to CROSS JOIN style?
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
The reason is that I want to create such query in Peoplesoft, and the above can only be achieved by creating a separate view for the selection with the group by clause. I want to do this just in one query without creating additional views.
You may try writing your query as a single level join with an aggregation:
SELECT
CT.GROUP,
CT.EMP_ID,
MAX(HT.EFF_DT) AS EFF_DT
FROM CURR_TABLE CT
LEFT JOIN HIST_TABLE HT
ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID AND
HT.STAT = 'A'
WHERE
CT.GROUP = :1 AND
CT.EMP_ID = :2
GROUP BY
CT.GROUP,
CT.EMP_ID;
Note that GROUP is a reserved SQL keyword, and you might have to escape it with double quotes to make this query (or the one in your question) work on Oracle.
I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id
I need the fields in 1 table contingent on 1 property matching rows in another table.
I can write this query in SQL with a subquery as such:
SELECT *
FROM Table1
WHERE Property1 IN
(
SELECT Property1
FROM Table2
WHERE Property0 = 1
)
But I read here that it's less complicated and just as easy to write with a join, which I did. However, so far I'm unable to return just Table1 as I'd like since I'm using a join, which if I'm not mistaken, requires me to create this anonymous type, as below. What I did here works (I created another object with the same properties of Table1 I need), but I can't help thinking there's a better way to do this.
Table1.Join(Table2, t1 => t1.Property1, t2 => t2.Property1, (t1, t2) => new
{
t1.Property1,
t1.Property2,
t1.Property3
})
.Select(ob => new UnnecessaryObject
{
Property1 = ob.Property1,
Property2 = ob.Property2,
Property3 = ob.Property3
}
I also tried just creating a Table1 in the .Select part, but I got an error about explicit construction not being allowed.
Just to clarify, I'd like to be able to return the IQueryable of type Table1, which it seems like I ought to be able to do without having to create UnnecessaryObject...but I'm still pretty new to LINQ, so I'd appreciate any help you can offer. Thanks in advance.
You could just do:
from t1 in table1
join t2 in table2 on t1.property1 equals t2.property1
select t1;
That would return a collection of table1 objects. This assumes from your example table1 is a collection of table1 objects and table2 is a collection of table2 objects.
The best translation of your original query I can come up with is:
from item in context.Table1
where context.Table2
.Where(x => x.Property0 == 0)
.Any(x => x.Property1 == item.Property1)
select item
This selects all items from Table1, where there's an item with matching Property1 and Property0 == 0 from Table2
It can also be solved with a join indeed. To get an efficient join, you need to have a relation between the two tables. Then you can do something like assuming the relation is called RelatedItems:
from item in context.Table1
join relatedItem in item.RelatedItems
on item.Property1 equals relatedItem.Property
where relatedItem.Property0 == 0
select item
This is equivalent to the SQL:
SELECT *
FROM Table1
JOIN Table2 ON Table1.Property1 = Table2.Property1
WHERE Table2.Property0 = 0
am a newbie to Oracle/PL SQL.I've 2 tables A and B.
A has a column CustId,Age,Location and Date. Table B has 2 columns CustId,CustName.
What would be the sql query to show show CustName and Location for a given age?
Thanks.
your question "What would be the sql query to show show CustName and Location for a given age?" helps define your query pretty well:
SELECT CustName, Location
FROM TableA a
INNER JOIN TableB b
ON b.CustId = a.CustId
WHERE a.Age = #
All we need to do on top of that select for your specific fields is make sure to join the two tables on their common column (CustID).
Another option would be to avoid the WHERE statement:
SELECT CustName, Location
FROM TableB b
INNER JOIN TableA a
ON a.CustID = b.CustID
AND a.Age = #
you need join. something like
SELECT custname, location FROM a JOIN b ON a.custid = b.custid WHERE age = [age];