subquery

Using 'LIKE' operator with a subquery that returns multiple results

久未见 提交于 2019-12-24 01:14:33
问题 Newbie to SQL. Kindly help. I need to count number of records which have a pattern in one of the fields, for multiple patterns. I know how to do it for one pattern, but how do I get count of each pattern when there are multiple patterns coming from a subquery. I am using Oracle. I will try to explain with an example. SELECT count(*) FROM TableA WHERE TableA.comment LIKE '%world%'; Now this code will return the number of records which have 'world' anywhere in the TableA.comment field. My

nhibernate queries SubQueryExpression

独自空忆成欢 提交于 2019-12-24 00:48:29
问题 Can someone explain me what are NHibernate SubQueryExpression based queries. Any links with concrete examples are very welcome. Thanks Update: Let's say that I have one entity named Beach. That beach can have many images. I want to select Beach entity and it;s first image from Images collection. I want to carry arround only that selected image object, or if I select only second object to carry only that object. I do not want to access like Images.First() cause that will initialize all

CakePHP Subquery from SQL

会有一股神秘感。 提交于 2019-12-24 00:17:33
问题 CREATE TABLE IF NOT EXISTS `messages` ( `id` int(11) unsigned NOT NULL auto_increment, `user_id` int(11) unsigned NOT NULL, `node_id` int(11) unsigned NOT NULL, `reciever_id` int(11) unsigned NOT NULL, `created` datetime default NULL, `modified` datetime default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ; INSERT INTO `messages` (`id`, `user_id`, `node_id`, `reciever_id`, `created`, `modified`) VALUES (1, 1, 1, 15, '2011-12-07 00:00:00', '2011-12-07 02:00

Optimize sub-query selecting last record of each group

自闭症网瘾萝莉.ら 提交于 2019-12-24 00:16:25
问题 I have this query which is a dependant query and taking much execution time SELECT u.id, u.user_name, ifnull((select longitude from map where user_id = u.id order by map_id desc limit 1 ),0) as Longitude, ifnull((select latitude from map where user_id = u.id order by map_id desc limit 1 ),0) as Longitude, (select created from map where user_id = 1 order by created desc limit 1) as LatestTime FROM users as u WHERE id IN(SELECT user1_id FROM relation WHERE users.id = 1) ORDER BY id; I tried

Impact of ordering of correlated subqueries within a projection

和自甴很熟 提交于 2019-12-23 20:23:30
问题 I'm noticing something a bit unexpected with how SQL Server (SQL Server 2008 in this case) treats correlated subqueries within a select statement. My assumption was that a query plan should not be affected by the mere order in which subqueries (or columns, for that matter) are written within the projection clause of the select statement. However, this does not appear to be the case. Consider the following two queries, which are identical except for the ordering of the subqueries within the

How can I combine SQL queries with different expressions?

不问归期 提交于 2019-12-23 19:28:47
问题 I've got three queries that are already at the peak of my SQL knowledge (Microsoft SQL 2005, if that matters) - and now I need to combine them into a single query with all of the values on a single row. My actual queries are below, but I thought it'd be easier if I provided a simple version here: Query One: -- Provides School District summary based on a CountyID SELECT DistrictID, Count(Schools) as NumberofSchools FROM Schools WHERE (CountyID = 207) GROUP BY DistrictID Query One Sample Output

NHibernate QueryOver SQLFunction in where clause

天大地大妈咪最大 提交于 2019-12-23 17:53:04
问题 I would like to query a table having multiple rows, each with a timestamp with data coming at ten minute intervals. I would like to find the beginning of any missing data, which is where there is not a timestamp equaling the next ten minute interval, like this: select a.[timestamp] from [table] as a where not exists (select 1 from [table] as b where a.[id] = b.[id] and b.[timestamp] = dateadd(mi, 10, a.[timestamp])) order by a.[timestamp] I have this so far, but I fail to see how to build the

Using a subquery for a column with QueryOver

左心房为你撑大大i 提交于 2019-12-23 17:33:48
问题 I'm trying to get something similar to the SQL below via QueryOver: SELECT docs.*, (SELECT TOP 1 eventDate from events WHERE id=docs.id AND type=4 ORDER BY eventDate DESC) as eventDate FROM documents as docs WHERE doc.accountId = ... I've got close with a projection, however I'm not sure how to get the entire documents table back. Documents has a one-to-many relationship with Events, I don't want to outer join as it will bring multiple results, and an inner join may not bring back a row: var

Subquery with “ANY” and local array generate nested too deep SQL Statement

假如想象 提交于 2019-12-23 15:33:38
问题 public IEnumerable<Table1> GetMatchingTable1(string param, double[] Thicknesses) { return DBContext.Table1.Where(c => c.Field1 == param && Thicknesses.Any(Thickness => Thickness >= c.MinThickness && Thickness <= c.MaxThickness)) .ToList(); } Above query return the following exception. "Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries." So far, all my research on the web for this error pointed toward replacing "ANY" with "CONTAINS".

Whether Inner Queries Are Okay?

无人久伴 提交于 2019-12-23 14:41:09
问题 I often see something like... SELECT events.id, events.begin_on, events.name FROM events WHERE events.user_id IN ( SELECT contacts.user_id FROM contacts WHERE contacts.contact_id = '1') OR events.user_id IN ( SELECT contacts.contact_id FROM contacts WHERE contacts.user_id = '1') Is it okay to have query in query? Is it "inner query"? "Sub-query"? Does it counts as three queries (my example)? If its bad to do so... how can I rewrite my example? 回答1: Your example isn't too bad. The biggest