subquery

How to get other column value in different table into the query?

允我心安 提交于 2019-12-10 10:09:40
问题 I had searching application, finding personal information which had been filtered by some criteria (category, years of experience etc) I had problem with the last filter, 'tempoh perkhidmatan by negeri'. I need to calculate the number of working experience by state(negeri). For example, when searching for people of 5 years in the state(negeri) 'x', the sql will sum years of experience of each person in the state selected. This is the full code of SQL searching by criteria: $query = DB::table(

SQL: “NOT IN” alternative for selecting rows based on values of *different* rows?

♀尐吖头ヾ 提交于 2019-12-10 02:22:19
问题 How do you make an SQL statement that returns results modified by a subquery, or a join - or something else, that deals with information you're trying to return? For example: CREATE TABLE bowlers ( bowling_id int4 not null primary key auto_increment, name text, team text ); Someone might incorrectly be on more than one team: INSERT INTO `bowlers` (`name`, `team`) VALUES ('homer', 'pin pals'), ('moe', 'pin pals'), ('carl', 'pin pals'), ('lenny', 'pin pals'), ('homer', 'The homer team'), ('bart

Write subquery in Criteria of nHibernate

此生再无相见时 提交于 2019-12-09 17:09:24
问题 I've read about subquery in Criteria, but I am still unable to grasp it properly. Here I am taking one example and if somebody can help me write that using subquery it will be great. Lets say we have table Employee{EmployeeId.(int),Name(string),Post(string),No_Of_years_working(int)} Now I want all the employees who are Managers and working for less than 10 years. I know that we can get the result without using subqueries but I want to use subquery just to understand how it works in criteria.

How to compare two query results for equality in MySQL?

好久不见. 提交于 2019-12-09 16:28:38
问题 I'm a MySQL user. I have two queries, and I wish to compare their results for equality . I would like to do this with a single query which would return true or false, so each of my two queries would most likely take the form of sub-queries. I would like to avoid returning the results from both queries and comparing them at the application level, to cut down on communication and to improve performance. I would also like to avoid looping over the results at the database level if possile, but if

in postgres select, return a column subquery as an array?

五迷三道 提交于 2019-12-09 15:47:02
问题 (have done this before, but memory fades, as does goggle) wish to get select from users with the tag.tag_id s for each user returned as an array. select usr_id, name, (select t.tag_id from tags t where t.usr_id = u.usr_id) as tag_arr from users u; with the idea embedded query tag_arr would be an array 回答1: Use the aggregate function: select usr_id, name, array_agg(tag_id) as tag_arr from users join tags using(usr_id) group by usr_id, name or an array constructor from the results of a subquery

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

[亡魂溺海] 提交于 2019-12-09 13:46:18
问题 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 )

Calculated Column Based on Two Calculated Columns

核能气质少年 提交于 2019-12-09 13:37:19
问题 I'm trying to do a rather complicated SELECT computation that I will generalize: Main query is a wildcard select for a table One subquery does a COUNT() of all items based on a condition (this works fine) Another subquery does a SUM() of numbers in a column based on another condition. This also works correctly, except when no records meet the conditions, it returns NULL . I initially wanted to add up the two subqueries, something like (subquery1)+(subquery2) AS total which works fine unless

Select last 30 items per group by

拜拜、爱过 提交于 2019-12-09 06:42:25
Hopefully the title makes any sense. For this example I'll have the next table in my database measurements ================================== stn | date | temp | time = 1 | 01-12-2001 | 2.0 | 14:30 = 1 | 01-12-2001 | 2.1 | 14:31 = 1 | 03-12-2001 | 1.9 | 21:34 = 2 | 01-12-2001 | 4.5 | 12:48 = 2 | 01-12-2001 | 4.7 | 12:49 = 2 | 03-12-2001 | 4.9 | 11:01 = ================================== And so on and so forth. Each station (stn) has many measurements, one per day second . Now I want to select the temp of each station of the last 30 days measurements where the station has at least 30

How to use regexp on the results of a sub query?

喜夏-厌秋 提交于 2019-12-08 18:29:24
I have two tables. User which has id and phone number id phone_no 1 ---- 9912678 2 ---- 9912323 3 ---- 9912366 Admission Table , which has id phone number id phone_no 6 --- 991267823 7 --- 991236621 8 --- 435443455 9 --- 243344333 I want to find all the phone number of Admission's table which has same pattern as users table and update it in users table. So i am trying this select phone_no from admission where phone_no REGEXP (SELECT phone_no FROM `users` AS user WHERE user.phone_no REGEXP '^(99)+[0-9]{8}') But i am getting this error Subquery returns more than 1 row Looking for help. Try one

How to reduce many similar correlated subqueries?

大憨熊 提交于 2019-12-08 18:28:27
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 this, or is this a normal looking statement? SELECT j.EmployeeId ,j.CompanyId ,j.JobCode ,COALESCE(j