subquery

Sql Server - user CTE in subquery

懵懂的女人 提交于 2019-12-08 17:41:48
问题 This question has been asked before - How we can use CTE in subquery in sql server? The only answer suggested was "Just define your CTE on top and access it in the subquery?" This works, but I would really like to be able to use a CTE in the following scenarios - as a subquery in a SELECT as a derived table in the FROM clause of a SELECT Both of these work in PostgreSQL. With Sql Server 2005, I get "Incorrect syntax near the keyword 'with'". The reason I would like it is that most of my

Doctrine2: Limiting with Left Joins / Pagination - Best Practice

拟墨画扇 提交于 2019-12-08 17:19:15
问题 i have a big query (in my query builder) and a lot of left joins. So i get Articles with their comments and tags and so on. Let's say i have the following dql: $dql = 'SELECT blogpost, comment, tags FROM BlogPost blogpost LEFT JOIN blogpost.comments comments LEFT JOIN blogpost.tags tags'; Now let's say my database has more than 100 blogposts but i only want the first 10, but with all the comments of those 10 and all their tags, if they exist. If i use setMaxResults it limits the Rows. So i

How to select data from multiple tables using joins/subquery properly? (PHP-MySQL)

旧街凉风 提交于 2019-12-08 16:56:48
问题 I have three tables as shown in below image. Note: Lead column of projectheader table stores an employee id. What I want to have is be able to retrieve something like the one in table my goal(Lead, displays the lead name of that employee) I was able to do that using the query below. SELECT DISTINCT projectdetails.ProjectDetailsID, projectheader.ProjectID, projectheader.ProjectName, projectheader.Lead, projectheader.StartDate, projectheader.EndDate, projectheader.Status, projectheader.Remarks,

Unknown column in mysql subquery

吃可爱长大的小学妹 提交于 2019-12-08 15:44:01
问题 I am trying to get the avg of an item so I am using a subquery. Update : I should have been clearer initially, but i want the avg to be for the last 5 items only First I started with SELECT y.id FROM ( SELECT * FROM ( SELECT * FROM products WHERE itemid=1 ) x ORDER BY id DESC LIMIT 15 ) y; Which runs but is fairly useless as it just shows me the ids. I then added in the below SELECT y.id, (SELECT AVG(deposit) FROM (SELECT deposit FROM products WHERE id < y.id ORDER BY id DESC LIMIT 5)z)

Mysql Inner join with OR condition?

南楼画角 提交于 2019-12-08 15:24:22
问题 I have 2 tables like below location_distance ---------------------------------------------- id | fromLocid | toLocid | distance ---------------------------------------------- 1 | 3 | 5 | 70 2 | 6 | 8 | 15 3 | 2 | 4 | 63 ... other_table -------------------------------------------- Id | fromLocid | toLocid | otherdata -------------------------------------------- 12 | 5 | 3 | xxxx 22 | 2 | 4 | xxxx 56 | 8 | 6 | xxxx 78 | 3 | 5 | xxxx I would like to retrieve the distance b/w the locations in

Alternative to using a subquery - can't create view in Mysql

泄露秘密 提交于 2019-12-08 13:21:06
问题 I have created a view that pulls a range of rows based on a calculation; using this script (per this question: How to select Range of rows based on field values - MySQL : select t.* from curdataEvents t cross join (select max(revs) as maxrev from curdataEvents) x where t.revs >= x.maxrev - 100000; This pulls the range of rows I need. In order to get the desired report - I have to create multiple views each creating the next layer of the report. The problem is MySQL won't create a view using a

How to reduce many similar correlated subqueries?

眉间皱痕 提交于 2019-12-08 07:12:52
问题 This is part of a larger statement, but I'm wondering if CTE or another method would help make this more efficient or cleaner. I could write it as a table-valued function and include it in my from clause, but I'd like to avoid extra objects if there is another solution. The SELECT TOP 1 ... sub-queries here simply catch when I have a rate with an earlier effective date than the base table, but I'm not fond of repeating them for each column I need to access. Is there a better way to accomplish

Microsoft SQL Server - restricting subqueries

自作多情 提交于 2019-12-08 06:49:46
问题 This is mostly a curiosity question. I've just experienced a situation where on a test database I had the following query: update table set column1 = 1 where column2 in (1,2) But this kept executing with the error that subquery returned more than one value. Now I checked to make sure I did not have multiple identity keys or that the 'in' values were unique. so for all intents and purposes this should not have happened. Checking on the LIVE copy of the database, same query did not have an

Query returning one “extra” record. Any advice on how to remove it from the query results?

若如初见. 提交于 2019-12-08 06:45:37
问题 We have the following, quite complex (at least for us) query. Since, as far as we know, there's no such thing as INTERSECT on MySQL, we are wondering how can we fix this: ( SELECT GROUP_CONCAT(APA_T.district ORDER BY APA_T.district), t.name FROM tbl_activity AS t INNER JOIN tbl_activity_package AS ap ON t.id = ap.id_activity INNER JOIN ( SELECT DISTINCT apa.district AS district, ( SELECT s1.id_activity_package FROM tbl_activity_package_address s1 WHERE apa.district = s1.district ORDER BY s1

Force outer SELECT to fail if the inner SELECT contains an invalid identifier

混江龙づ霸主 提交于 2019-12-08 06:05:17
问题 If SELECT ID FROM T2 fails with the following message: Error: ORA-00904: "ID": invalid identifier why doesn't SELECT * FROM T1 WHERE ID IN ( SELECT ID FROM T2 ) fail ? (it returns all entries from T1) Is it possible to change this default behavior? (running the same query, but getting an error instead of all rows) I have: T1 with ID as column T2 with ID2 as column (T2 doesn't contain ID) but let's say that I use SELECT ID FROM T2 (see the example above) instead of SELECT ID2 FROM T2 by