derived-table

Laravel 5, Derived table in join clause?

本秂侑毒 提交于 2019-12-07 05:30:44
问题 I have this query: SELECT * FROM blog LEFT JOIN ( SELECT blog_id, AVG(value) as blog_rating FROM blog_ratings GROUP BY (blog_id) ) T ON T.blog_id = blog.id; I do not know how to write this with Eloquent. For Example: Blog::select("*")->leftJoin( /* Here goes derived table */ )->get() How do I accomplish this? 回答1: I'd personally just use the fluent query builder, try this out and see how it works out: DB::table('blog') ->select('*') ->leftJoin(DB::raw('(SELECT blog_id, AVG(value) as blog

Subquery's rand() column re-evaluated for every repeated selection in MySQL 5.7/8.0 vs MySQL 5.6

删除回忆录丶 提交于 2019-12-03 20:31:35
I am doing a subquery in which I have a calculated column involving random number generation. In the base query I select this column twice. MySQL 5.6 works as I expect, the calculated value being called once and fixed. The 5.7+/8.0+ execution seems to re-evaluate the subquery's column value individually for each selection. Is this correct behavior? What can I do to force it work as expected in newer versions of MySQL? CREATE TABLE t ( `id` BIGINT(20) NOT NULL PRIMARY KEY AUTO_INCREMENT ) ENGINE=InnoDB; insert into t values(); insert into t values(); insert into t values(); insert into t values

How do I tell the MySQL Optimizer to use the index on a derived table?

馋奶兔 提交于 2019-12-03 10:58:30
问题 Suppose you have a query like this... SELECT T.TaskID, T.TaskName, TAU.AssignedUsers FROM `tasks` T LEFT OUTER JOIN ( SELECT TaskID, GROUP_CONCAT(U.FirstName, ' ', U.LastName SEPARATOR ', ') AS AssignedUsers FROM `tasks_assigned_users` TAU INNER JOIN `users` U ON (TAU.UserID=U.UserID) GROUP BY TaskID ) TAU ON (T.TaskID=TAU.TaskID) Multiple people can be assigned to a given task. The purpose of this query is to show one row per task, but with the people assigned to the task in a single column

How do I tell the MySQL Optimizer to use the index on a derived table?

守給你的承諾、 提交于 2019-12-03 01:35:34
Suppose you have a query like this... SELECT T.TaskID, T.TaskName, TAU.AssignedUsers FROM `tasks` T LEFT OUTER JOIN ( SELECT TaskID, GROUP_CONCAT(U.FirstName, ' ', U.LastName SEPARATOR ', ') AS AssignedUsers FROM `tasks_assigned_users` TAU INNER JOIN `users` U ON (TAU.UserID=U.UserID) GROUP BY TaskID ) TAU ON (T.TaskID=TAU.TaskID) Multiple people can be assigned to a given task. The purpose of this query is to show one row per task, but with the people assigned to the task in a single column Now... suppose you have the proper indexes setup on tasks , users , and tasks_assigned_users . The

Pull columns from derived table and sum them in one MySQL SELECT statement

橙三吉。 提交于 2019-12-02 12:00:21
I need to pull column data from two tables, run calculations on the data with the result saved as an alias, and then sum those results into other alias' to display in a php table. I am trying to achieve this by creating a derived table within my SELECT statement but it doesn't work. I don't receive any errors but my table only displays the column headers. CODE: $sql = "SELECT x.company, x.stagestatus, x.shippeddate, SUM(x.totprice) as totalprice, SUM(x.sgtotquantity) as sgtotqty, SUM(x.sgtotalsqft) as sgtotsqft, SUM(x.avgsqftrev) as avgsqftrevenue, SUM(x.avgunitrev) as avgunitrevenue FROM

Error Code: 1248. Every derived table must have its own alias No solution found for query

送分小仙女□ 提交于 2019-12-01 17:08:45
问题 I am getting an error while using this query in MySQL. The query logic is correct and I have tried it in Oracle and it's running fine, but I'm getting an error when running this in MySQL. I looked at previous questions on StackOverflow, but didn't find something to help me. Here is the query: select * from (select PM.ID, PM.Name, PM.TIMEOUT, PMS.PROCESS_MONITOR_ID, PMS.PROCESS_START_DATE from RATOR_IMP.PROCESS_MONITOR as PM JOIN RATOR_IMP.PROCESS_MONITOR_STATISTIC as PMS ON PM.ID = PMS

SQL - Relationship between a SubQuery and an Outer Table

不打扰是莪最后的温柔 提交于 2019-11-30 22:38:26
问题 Problem I need to better understand the rules about when I can reference an outer table in a subquery and when (and why) that is an inappropriate request. I've discovered a duplication in an Oracle SQL query I'm trying to refactor but I'm running into issues when I try and turn my referenced table into a grouped subQuery. The following statement works appropriately: SELECT t1.* FROM table1 t1, INNER JOIN table2 t2 on t1.id = t2.id and t2.date = (SELECT max(date) FROM table2 WHERE id = t1.id)

SELECT INTO USING UNION QUERY

与世无争的帅哥 提交于 2019-11-29 20:56:34
I want to create a new table in SQL Server with the following query. I am unable to understand why this query doesn't work. Query1: Works SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2 Query2: Does not Work. Error: Msg 170, Level 15, State 1, Line 7 Line 7: Incorrect syntax near ')'. SELECT * INTO [NEW_TABLE] FROM ( SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2 ) Thanks! You have to define a table alias for a derived table in SQL Server: SELECT x.* INTO [NEW_TABLE] FROM (SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2) x "x" is the table alias in this example. INSERT INTO #Temp1 SELECT val1

Derived account balance vs stored account balance for a simple bank account?

天大地大妈咪最大 提交于 2019-11-29 18:45:18
So, its like our normal bank accounts where we have a lot of transactions which result in inflow or outflow of money. The account balance can always be derived by simply summing up the transaction values. What would be better in this case, storing the updated account balance in the database or re-calculating it whenever needed? Expected transaction volume per account: <5 daily Expected retrieval of account balance: Whenever a transaction happens and once a day on an average otherwise. How would you suggest to take a decision on this? Thanks a lot! PerformanceDBA Preface Audit requirements are

SELECT INTO USING UNION QUERY

纵然是瞬间 提交于 2019-11-28 17:08:36
问题 I want to create a new table in SQL Server with the following query. I am unable to understand why this query doesn't work. Query1: Works SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2 Query2: Does not Work. Error: Msg 170, Level 15, State 1, Line 7 Line 7: Incorrect syntax near ')'. SELECT * INTO [NEW_TABLE] FROM ( SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2 ) Thanks! 回答1: You have to define a table alias for a derived table in SQL Server: SELECT x.* INTO [NEW_TABLE] FROM (SELECT * FROM