subquery

MySQL getting MAX of previous WHERE condition

I have a table: id | score | date bob | 40 | 2010-1-1 bob | 70 | 2010-1-15 sue | 55 | 2010-1-1 sue | 80 | 2010-2-1 I want to query for either the score for a user on a specific date OR, if no score exists for that user on that date,return the score from the most recent date for that user. Is there a way to do this w...

Can this this MySQL query (with subqueries) be optimized?

Is there any better way to make this query work? I'm looking for a more efficient solution, if there is one available. SELECT `unitid`, `name` FROM apartmentunits WHERE aptid IN ( SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 4 AND condnum = 1 ) AND aptid IN ( SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 2...

Doctrine: How to do a UPDATE with a SELECT MIN subquery?

Hi, I have a photos table where users can have multiple photos. I'm trying to create the following query: $q = Doctrine_Query::create() ->update('Photo p') ->set('p.photo_type', 1) ->where('p.user_id = ?', $id) ->andWhere('p.date_added = (SELECT MIN(p.date_added) FROM Photo p WHERE p.user_id = ?)', $id) The idea is to se...

Subquery and selecting oldest rows for multiple foreign keys in MySQL

I have two tables: product (idproduct, name, description, tax) product_storage (idstorage, idproduct, added, quantity, price) for each product it could be different price and oldest are "first-to-sell" for example in storage I have: 1, 1, 2010-01-01, 0, 10.0 2, 1, 2010-01-02, 0, 11.0 3, 1, 2010-01-03, 10, 12.0 4, 2, 2010-01-04, ...

LINQ query help needed for Intersect

Hi, LINQ gurus, I am looking for help to write a query... I have a table with Person records, and it has a nullable ParentID column, so it is kind of self-referencing, where each record might have a Parent. I am looking for unprocessed rows whose parent rows were processed. This SQL works fine: SELECT * FROM Person where IsProce...

How to construct NHibernate criteria to find parents who has all specified children

A Project can have many Parts. A property on Part is Ipn, which is a string of digits. Project "A" has Parts "1", "2", "3" Project "B" has Parts "2", "3", "4" Project "C" has Parts "2" Project "D" has Parts "3" I want to find all Projects that have all of the specified parts associated with it. My current query is var ...

SQL query to retrieve from three inter related tables

I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator is the creator of the group) Association(Id, UserId, GroupId) (entries in this table include which user is in which group) Association.UserID =Users.Id, Association.GroupId = Groups.id and also Groups.creator = Users.Id....

Can we write subquery in between SELECT and FROM

i want to know, how to write subquery in between SELECT and FROM as SELECT Col_Name,(Subquery) From Table_Name Where Some_condition ...

Need Help with SQL Subquery

Hey: I am trying to write a query that will return all orders that only have a Subscription included. It is easy enough to write a query that includes all Orders with Subscriptions, another that includes all orders without a Subscription and then compare them with an unmatched query. But I don't want to have to store Queries in my Acce...

mysql reference result from subquery

this is what i am doing update t1 set x=a,y=b where a and b are obtained from (select query here) i know the select query the select query returns multiple results which are the same when i use group by or distinct query execution slows down considerably a and b are forward references so mysql reports an error i want to set a equal t...

Speeding up inner-joins and subqueries while restricting row size and table membership

I'm developing an rss feed reader that uses a bayesian filter to filter out boring blog posts. The Stream table is meant to act as a FIFO buffer from which the webapp will consume 'entries'. I use it to store the temporary relationship between entries, users and bayesian filter classifications. After a user marks an entry as read, it w...

Multiple column subselect in mysql 5 (5.1.42)

This one seems to be a simple problem, but I can't make it work in a single select or nested select. Retrieve the authors and (if any) advisers of a paper (article) into one row. I order to explain the problem, here are the two data tables (pseudo) papers (id, title, c_year) persons (id, firstname, lastname) plus a link table w/one e...

Joins and subqueries in LINQ

I am trying to do a join with a sub query and can't seem to get it. Here is what is looks like working in sql. How do I get to to work in linq? SELECT po.*, p.PermissionID FROM PermissibleObjects po INNER JOIN PermissibleObjects_Permissions po_p ON (po.PermissibleObjectID = po_p.PermissibleObjectID) INNER JOIN Permissions p ON (po_p.P...

Avoiding repeated subqueries when 'WITH' is unavailable

MySQL v5.0.58. Tables, with foreign key constraints etc and other non-relevant details omitted for brevity: CREATE TABLE `fixture` ( `id` int(11) NOT NULL auto_increment, `competition_id` int(11) NOT NULL, `name` varchar(50) NOT NULL, `scheduled` datetime default NULL, `played` datetime default NULL, PRIMARY KEY (`id`) ); ...

Using subselect to accomplish LEFT JOIN

Is is possible to accomplish the equivalent of a LEFT JOIN with subselect where multiple columns are required. Here's what I mean. SELECT m.*, (SELECT * FROM model WHERE id = m.id LIMIT 1) AS models FROM make m As it stands now doing this gives me a 'Operand should contain 1 column(s)' error. Yes I know this is possible with LEFT JOI...

Need some help with a MySQL subquery count

I'm running into my own limits of MySQL query skills, so I hope some SQL guru can help out on this one. The situation is as follow: I have images that can be tagged. As you might expect this is stored in three tables: Image Tag Tag_map (maps images to tags) I have a SQL query that calculates the related tags based on a tag id. The q...

How to write a subquery using DB

SELECT SUM( amount_disbursed ) disbusedamount, (select SUM( loaninstallmentpaid_amount ) FROM ourbank_loan_repayment) paidamount, SUM( amount_disbursed )- (select SUM( loaninstallmentpaid_amount ) FROM ourbank_loan_repayment) differenceamount FROM ourbank_loan_disbursement Any Help would be appreciated ...

How do I avoid a repetitive subquery JOIN in SQL?

Hi In SQL Server 2008: I have one table, and I want to do something along the following lines: SELECT T1.stuff, T2.morestuff from ( SELECT code, date1, date2 from Table ) as T1 INNER JOIN ( SELECT code, date1, date2 from Table ) as T2 ON T1.code = T2.code and T1.date1 = T2.date2 The two subqueries are exactly identical. Is there...

Can this MySQL subquery be optimised?

I have two tables, news and news_views. Every time an article is viewed, the news id, IP address and date is recorded in news_views. I'm using a query with a subquery to fetch the most viewed titles from news, by getting the total count of views in the last 24 hours for each one. It works fine except that it takes between 5-10 seconds ...

Help with SQL query (list strings and count in same query)

Hi everybody, I’m working on a small kind of log system to a webpage, and I’m having some difficulties with a query I want to do multiple things. I have tried to do some nested / subqueries but can’t seem to get it right. I’ve two tables: User = {userid: int, username} Registered = {userid: int, favoriteid: int} What I need is a q...