subquery

predicate subquery to return items by matching tags

此生再无相见时 提交于 2019-12-11 21:02:08
问题 I have a many-to-many relationship between two entities; Item and Tag. I'm trying to create a predicate to take the selectedItem and return a ranking of items based on how many similar tags they have. So far I've tried: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(itemToTag, $item, $item in %@).@count > 0", selectedItem.itemToTag]; Any other iterations that have failed. It currently only returns the selectedItem in the list. I've found little on Subquery. Is there a

MySQL - can I avoid these correlated / dependant subqueries?

岁酱吖の 提交于 2019-12-11 20:39:17
问题 I have a MySQL query that I have been optimising, and currently it has 2 dependent / correlated subqueries. I was wondering if it was possible to re write to avoid these? SELECT * FROM `pp_slides` JOIN `pp_slide_content` ON `pp_slides`.`id` = `pp_slide_content`.`slide_id` AND `pp_slide_content`.`version` = ( SELECT max(`version`) FROM `pp_slide_content` WHERE `slide_id` = `pp_slides`.`id` ) LEFT JOIN `pp_published_slides` ON `pp_published_slides`.`slide_id` = `pp_slides`.`id` AND `pp

A better way to modify this query so the subquery will be more specific?

南楼画角 提交于 2019-12-11 20:34:58
问题 This is the current query that I was working with and it was doing a good job until now: SELECT a.* FROM ads AS a WHERE id = ( SELECT id FROM ads AS b WHERE a.zone_name=b.zone_name AND publish_date <= CURDATE() AND expire_date >= CURDATE() AND active = 1 LIMIT 1 ) It returns one ad per every existing zone, and then I display the appropriate ad in the appropriate place in my template after sorting them by zones. Now we have introduced a responsive layout, so the zones can't always fit the same

WHERE clause in sub-query

╄→гoц情女王★ 提交于 2019-12-11 20:18:38
问题 Following SQL query: select * from er_101 where cd_relnaam IN ( select cd_relnaam from er_101 group by cd_relnaam having count(*) > 1) AND ld_relopdrachtgever = '1' Though I need to have that sub query also limits on ld_relopdrachtgever = '1' How is that possible with an HAVING statement? 回答1: You can also use WHERE in sub-query. SELECT * FROM er_101 WHERE cd_relnaam IN ( SELECT cd_relnaam FROM er_101 WHERE ld_relopdrachtgever = '1' <--You can add WHERE clause before GROUP BY --^^^^^----

Django subquery in insert

懵懂的女人 提交于 2019-12-11 19:18:16
问题 Is it possible to force django to make a subquery when I try to insert some values? This produces two separate queries: CommunicationLog.objects.create( device='asdfasdf', date_request=CommunicationLog.objects.get(pk=343).date_request, content_obj_id=338, type_request=1, type_change=2 ) 回答1: You definitely cannot do it by using create . There's no available API that will let you do it, since this is very unusual use case. You have to fall back to raw sql. 回答2: Even if the API won't let you do

Two subqueries added to addition complex mySQL query returning no records

a 夏天 提交于 2019-12-11 19:13:38
问题 I have this code, and individually they all work, so I think it must be the syntax which is incorrectly placed: SELECT i.*, o.organ_name, o.organ_logo, vtable.*, cc.ccount FROM heroku_056eb661631f253.op_ideas i JOIN The above collects all records from op_ideas table. (SELECT v.idea_Id, COUNT(v.agree = 1 or null) as agree, COUNT(v.disagree = 1 or null) as disagree, COUNT(v.obstain = 1 or null) as abstain FROM op_idea_vote v GROUP BY v.idea_id ) AS vtable ON vtable.idea_id = i.idea_id The above

Looking to create a subquery that displays only one column MYSQL

自古美人都是妖i 提交于 2019-12-11 19:04:45
问题 This database is for a book loan system. Where if a student takes a copy of a book the system accepts there student number and gives them a return date for the book. These are the two tables I am using loan table: CREATE TABLE loan ( `code` INT NOT NULL, `no` INT NOT NULL, taken DATE NOT NULL, due DATE NOT NULL, `return` DATE NULL, CONSTRAINT pri_loan PRIMARY KEY (taken), CONSTRAINT for_loan FOREIGN KEY (`code`) REFERENCES copy (`code`), FOREIGN KEY (`no`) REFERENCES student (`no`)); student

Mysql subselect alternative

妖精的绣舞 提交于 2019-12-11 18:56:46
问题 I have a query that I know can be done using a subselect, but due to large table sizes (100k+ rows per table) I would like to find an alternative using a join. This is not a homework question, but it's easier to share an example in such terms. Suppose there are two tables: Students :id :name 1 Tom 2 Sally 3 Ben Books :id :student_id :book 1 1 Math 101 2 1 History 3 2 NULL 4 3 Math 101 I want to find all students who don't have a history book. Working subselect is: select name from students

PGSQL - Joining two tables on complicated condition

北战南征 提交于 2019-12-11 18:54:48
问题 I got stuck during database migration on PostgreSQL and need your help. I have two tables that I need to join: drzewa_mateczne.migracja (data I need to migrate) and ibl_as.t_adres_lesny (dictionary I need to join with migracja). I need to join them on replace(drzewa_mateczne.migracja.adresy_lesne, ' ', '') = replace(ibl_as.t_adres_lesny.adres, ' ', ''). However my data is not very regular, so I want to join it on first good match with the dictionary. I've created the following query: select

Update with a subquery in MySQL

让人想犯罪 __ 提交于 2019-12-11 18:09:10
问题 I have an update query that I need to run in MySQL and am having some trouble with it. I have spent the last hour researching SO for a solution, but couldn't find one that actually worked. I need to do the following: UPDATE TABLE1 SET ID = (SELECT TABLE2.ID FROM TABLE2, TABLE1 WHERE TABLE1.NAME=TABLE2.NAME) WHERE TABLE1.ID IS NULL I have been getting the Error Code: 1242. Subquery returns more than 1 row error. How can I modify my query to make it run successfully? Basically, I need to fill