I have two tables with related with a many-to-many relationship. I am trying to find all of the rows that have no related values. Here's an example:
Table 1: Categories
Table 2: CategoryItems
Table 3: Items
I am trying to find all of the items that have no categories associated.
If I were writing this in straight SQL, this is what I would do:
SELECT *
FROM Items
WHERE Items.item_id not in (SELECT DISTINCT item_id FROM category_items)
This seems like a fairly straightforward requirement, but I can't figure out how to get Doctrine to do this.
Thanks for any suggestions!
You need to do a query like this SELECT * FROM items left join category_items on ... where cateogry_items.id is null. In Doctrine that means something like:
ItemsTable::getInstance()->createQuery('Items')
->leftJoin('Items.CategoryItems CategoryItems')
->where('CategoryItems.id IS NULL')
->execute()
Related
How can I only select specific columns in a polymorphic relationship because the table columns are different?
$query->with(['Postable' =>function($query){
$query->select('business_title','name ','last_name');
}
The business_title column exists in the agency table. Name and last_name exist in the user table. If I select one table other table gets an error of column not found.
Either develop a naming convention that fulfills your needs (ex: columns like 'name', 'title' can be found on many tables), or do not use select in your query and get all coulmns: $query->with(['Postable']); and you would need appropriate ways to read your query results. Or simply use multiple queries to read from different tables which seems to be the rational option.
I have two tables like bellow shows figures
I need to select records as bellow shown figure. with AH_ID need to join in second table and ATT_ID will be the column header and ATT_DTL_STR_VALUE need to get as that column relevant value
Required output
Sounds like you have an Entity-Attribute-Value data model which relational DBs aren't the best at modeling. You may want to look into a key-value store.
However, as Justin suggested, if you're using 11g you can use th pivot clause as follows:
SELECT *
FROM (
SELECT T1.AH_ID, T1.AH_DESCRIPTION, T2.ATT_ID, T2.ATT_DTL_STR_VALUE
FROM T1
LEFT OUTER JOIN T2 ON T1.AH_ID = T2.AH_ID
)
PIVOT (MAX(ATT_DTL_STR_VALUE) FOR (ATT_ID) IN (1));
This statement requires you to hard-code in ATT_ID however there are ways to do it dynamically. More info can be found here.
I got the following tables
TableA, TableB, TableC, TableD, TableE and they have foreign key relations like
FK_AB(one to many),FK_BC(one to one),FK_CD(One to many),FK_DE(one to one) and have the navigation properties based on these foreignkeys
Now I want to query TableA and get the records from TableA, TableD and TableE whoose Loadedby column equal to System. My query is like below
var query= from A in Context.TableA.Expand(TableB/TableC/TableD).Expand(TableB/TableC/TableD/TableE)
where A.Loadedby=="System"
select A;
The above query is working fine. I want the records from TableD and TableE whoose Loadedby value equal to System but the above query returning all the records from TableD and TableE which are related to TableA record satisfying A.Loadedby="System" this condition is not checked in the child tables.
Can anyone tell me how to filter the child tables also.
Currently OData only supports filters on the top-level. So in the above example it can only filter rows from the TableA. Inside expansions all the approriate rows will be included, always, there's no way to filter those right now.
You might be able to ask for the exanded entities separately with additional queries (with the right filter) and possibly use batch to group all the queries in one request. But that depends on the actual query you need to send.
I have got duplicate rows in a dataset. how can i select distinct rows from that.
From comments: My query is something like this:
select name, age
from student
When I receive its output in a dataset the output consists of rows having duplicate names. Using dataset itself I have to select distinct name from this because I need the same query with duplicate values for some other place.
select DISTINCT name, age from student
If you need both the distinct data as well as the full data (with duplicate values), then you'll either have to maintain two datasets or continue doing things as you are now.
I have 3 tables:
listing
photo
calendar
"photo" and "calendar" both have a "listing_id" column in them and every "listing" has a "photo". But I only want to select rows that have no entry in the "calendar" table with the matching "listing_id".
I'm not sure if I'm saying it correctly, but any help would be greatly appreciated. And if someone could show me CodeIgniter syntax, that'd be even better.
This will produce the list of calendar.free_date values that should not be returned because their associated listing_id values do not exist in the listing table.
select free_date from calendar c
where not exists (select * from listing
where listing_id = c.listing_id);
Should work as an SQL query. Not sure about CI syntax. Sorry!
SELECT * FROM listing WHERE listing_id NOT IN (SELECT listing_id FROM calendar)