subquery

MySQL Subquery LIMIT

As the title says, I wanted a workaround for this... SELECT comments.comment_id, comments.content_id, comments.user_id, comments.`comment`, comments.comment_time, NULL FROM comments WHERE (comments.content_id IN (SELECT content.content_id FROM content WHERE content.user_id = 1 LIMIT 0, 10)) Cheers ...

Linq query with subquery as comma-separated values

In my application, a company can have many employees and each employee may have have multiple email addresses. The database schema relates the tables like this: Company -> CompanyEmployeeXref -> Employee -> EmployeeAddressXref -> Email I am using Entity Framework and I want to create a LINQ query that returns the name of the company...

Are conditional subqueries optimized out, if the condition is false?

I have a table foo and a table bar, where each foo might have a bar (and a bar might belong to multiple foos). Now I need to select all foos with a bar. My sql looks like this SELECT * FROM foo f WHERE [...] AND ($param IS NULL OR (SELECT ((COUNT(*))>0) FROM bar b WHER...

Mysql query problem

Below is an over-simplified version of table I'm using: fruits +-------+---------+ | id | type | +-------+---------+ | 1 | apple | | 2 | orange | | 3 | banana | | 4 | apple | | 5 | apple | | 6 | apple | | 7 | orange | | 8 | apple | | 9 | apple | | 10 | banana | +...

subquery factoring questions.

Hi, Please explain. a) "subquery factoring" is used to replace a non-correlated subquery. What about correlated subquery? Is there any way to move a correlated sub-query to 'WITH' clause section? b) are "subquery" "subquery factoring" executed exactly once? c) "subquery" vs "subquery factoring" which one is better Thank you. ...

Hibernate Criteria and row count restriction

Hi, i have two entities named Parent and Child, linked in a one-to-many relationship. The Child entity has a boolean isStudent property. How do i get, using the Hibernate Criteria API, all the Parent entities that have at least one Child with isStudent = true? I was trying to use a Projection object to count all the parents that have...

linq subquery child collection to string

Hi All, i am trying to figure out how to write a linq query that will return a child collections "name" property as a string. I have a BO that has a "options" property where the options are the "name" property of each option in an "order" object. I would like the result to look something like order.id = 12312 order.date = 12/03/10 or...

Average of a Sum in Mysql query

I am having some problems creating a query that gives me the average of a sum. I read a few examples here in stackoverflow and couldn't do it. Can anyone help me to understand how to do this please? This is the data I have: Transaction_x0020_Number Product_x0020_Code Sales_x0020_Value Date Cashier 000356 350...

MYSQL UPDATE with IN and Subquery

Hi i have tables like this : table entry : id | total_comments ___________ 1 | 0 2 | 0 3 | 0 4 | 0 table comments : id | eid | comment ___________ 1 | 1 | comment sdfd 2 | 1 | testing testing 3 | 1 | comment text 4 | 2 | dummy comment 5 | 2 | sample comment 6 | 1 ...

Updating records with their subordinates via CTE or subquery

Let's say I have a table with the following columns: Employees Table employeeID int employeeName varchar(50) managerID int totalOrganization int managerID is referential to employeeID. totalOrganization is currently 0 for all records. I'd like to update totalOrganization on each row to the total number of employees under them. So ...

How to compare int list to SQL subquery

Hello, Is there a way to compare a list of numbers to a SQL subquery something like WHERE 9,10,11 = (SELECT tableID FROM ....) EDIT: I guess my question isn't clear but I'm sorry, I really don't know how else to say this. I'm expecting a list of IDS back from the subquery like 1,2,3 or at least that's what I'd like the result to look...

How to do a Postgresql subquery in select clause with join in from clause like SQL Server?

I am trying to write the following query on postgresql: select name, author_id, count(1), (select count(1) from names as n2 where n2.id = n1.id and t2.author_id = t1.author_id ) from names as n1 group by name, author_id This would certainly work on Microsft SQL Server but it does not at all on p...

The best way to return related data in a SQL statement

I have a question on the best method to get back to a piece of data that is in a related table on the other side of a many-to-many relationship table. My first method uses joins to get back to the data, but because there are multiple matching rows in the relationship table, I had to use a TOP 1 to get a single row result. My second met...

MYSQL join - reference external field from nested select?

Is it allowed to reference external field from nested select? E.g. SELECT FROM ext1 LEFT JOIN (SELECT * FROM int2 WHERE int2.id = ext1.some_id ) as x ON 1=1 in this case, this is referencing ext1.some_id in nested select. I am getting errors in this case that field ext1.some_id is unknow. Is it possible? Is there some other way? UP...

how to reuse subquery result in mysql

I'm doing a statistic job like this: SUM |COND1 |COND2 |... -------------------------- 100 |80 | 70 |... The SUM result is calculated from multiple tables, and the CONDs are subset of that. I wrote a sql like this: select tmp1.id,sum,cond1,cond2 from ( select id, count(*) as sum from table1 group by table1.id) tmp...

Invalid SQL Query

I have the next query that in my opinion is a valid one, but I keep getting error telling me that there is a proble on "WHERE em.p4 = ue.p3" - Unknown column 'ue.p3' in 'where clause'. This is the query: SELECT DISTINCT ue.p3 FROM table1 AS ue INNER JOIN table2 AS e ON ue.p3 = e.p3 WHERE EXISTS( SELE...

sql combine two subqueries

I have two tables. Table A has an id column. Table B has an Aid column and a type column. Example data: A: id -- 1 2 B: Aid | type ----+----- 1 | 1 1 | 1 1 | 3 1 | 1 1 | 4 1 | 5 1 | 4 2 | 2 2 | 4 2 | 3 I want to get all the IDs from table A where there is a c...

subqueries linq

Hey all I am trying to do a subquery in linq but the subquery is a value and it seems to not be working, can anyone help out? I am using the entit frame work I keep getting and int to string error not sure why. from lrp in remit.log_record_product join lr in remit.log_record on lrp.log_record_id equals l...

Update a field thanks to a subquery based on another field

Hi, I use a MySQL DB, and I would like to update a field in a table based on another. Something like: UPDATE table1 SET field1 = table2.id WHERE field2 IN ( SELECT table2.name FROM table2 ); I know that this query wouldn't work, but here is the idea. Is that even possible to do? Cheers, Nicolas. ...

HQL join after output of subquery

Hey, Say I have 3 classes class foo1 { List<foo2> prop1;} class foo2 { foo3 prop2;} class foo3{ int Id;} now I want to select distinct foo2's from foo1 and get Id of foo3. I need to write a HQL stmt for the same. I tried the following select c.Id from (select distinct(a.prop1) from foo1.prop1 as a ) as b inner join b.prop2 as c...