subquery

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

拟墨画扇 提交于 2019-12-23 02:44:09
问题 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 '^

Difference between ON and WHERE in subquery

跟風遠走 提交于 2019-12-23 02:41:01
问题 I found a weird disparity in mysql between using an ON and a WHERE to filter a subquery with a join. This query runs fine: SELECT * FROM cobrand co WHERE co.id IN ( SELECT co2.id FROM cobrand co3 INNER JOIN cobrand co2 ON co2.id = co3.id + 1 WHERE co2.id = co.id ) But this one returns an error Unknown column 'co.id' in 'on clause' : SELECT * FROM cobrand co WHERE co.id IN ( SELECT co2.id FROM cobrand co3 INNER JOIN cobrand co2 ON co2.id = co3.id + 1 AND co2.id = co.id ) Obviously the subquery

Updating multiple columns with data from subquery in MySQL

↘锁芯ラ 提交于 2019-12-23 02:31:14
问题 I am trying to update multiple columns in a row, with data from multiple columns in a subquery. The following approaches did not work for me, and I can't find different ones that suit my needs: UPDATE beers, (SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AVG(taste) AS taste, AVG(aroma) AS aroma, AVG(overall) AS overall, beer_id FROM reviews) AS review_total SET beers.appearance = review_total.appearance, beers.palate = review_total.palate, beers.taste = review_total.taste,

How to Add rows using subqueries in sqlalchemy?

萝らか妹 提交于 2019-12-23 02:18:53
问题 I'm using Postgresql with SQLAlchemy but it seems sqlalchemy is having trouble adding rows when using subqueries. In my example, I want to update a counter for a specific tag in a table. In SqlAlchemy a test run class would look like the following: class TestRun( base ): __tablename__ = 'test_runs' id = sqlalchemy.Column( 'id', sqlalchemy.Integer, sqlalchemy.Sequence('user_id_seq'), primary_key=True ) tag = sqlalchemy.Column( 'tag', sqlalchemy.String ) counter = sqlalchemy.Column( 'counter',

Subqueries with QueryOver

爱⌒轻易说出口 提交于 2019-12-22 14:02:32
问题 I have a issue in using subquery with queryover. This is what I have var address = QueryOver.Of<Address>() .Where(x => x.City.IsLike("%" + city + "%")).Select(x => x.Person.Id); var result = Session.QueryOver<Person>() .Where(x => x.Type.IsLike(type + "%")) .And(x => x.Name.IsLike("%" + name + "%")) .WithSubquery.WhereExists(address); I have a table for Person and a person has multiple addreses. So Person id, name, type and Address will have PersonId and city etc. So want to search a person

SQLite outer query is returning results not found in inner query

纵饮孤独 提交于 2019-12-22 09:41:24
问题 I just wondered if anyone has run into a case in SQLite (3.7.4) where a query would return one set of results, and when it becomes a subquery the results are completely different? I found the problem in a more complex query, but here's a simpler example that demonstrates the same behavior: Database setup: CREATE TABLE "test" ("letter" VARCHAR(1) PRIMARY KEY, "number" INTEGER NOT NULL); INSERT INTO "test" ("letter", "number") VALUES('b', 1); INSERT INTO "test" ("letter", "number") VALUES('a',

Very Slow MYSQL Sub Query

谁说我不能喝 提交于 2019-12-22 09:39:05
问题 Guys, im more of a MSSQL guy but im working on some MYSQL right now. Iv written a simple query, with a subquery and I cant understand for the life of me why its so slow. This query: SELECT MAX(timestamp), user, status FROM checkin WHERE room_id = 'Room Name' AND timestamp > DATE_SUB(Now() ,INTERVAL 4005 SECOND) GROUP BY user Runs in 0.0034 seconds Yet this relatively similiar query but nested, takes over 6 seconds .. SELECT user, status FROM checkin WHERE timestamp IN (SELECT MAX(timestamp)

if a non-correlated subquery is repeated at several places in the query, can it be cached and the result reused?

99封情书 提交于 2019-12-22 07:03:14
问题 If I have a query like SELECT date_trunc('day', assigndate)e, count(CASE WHEN a.assigneeid = 65548 AND a.assigneeid IN (SELECT userid FROM groupmembers WHERE groupid = 65553) THEN 1 ELSE NULL END) assigned, count(CASE WHEN a.assigneeid = 65548 AND a.completedtime IS NOT NULL AND a.assigneeid IN (SELECT userid FROM groupmembers WHERE groupid = 65553) THEN 1 ELSE NULL END) completed FROM ASSIGNMENT a WHERE assigndate > CURRENT_TIMESTAMP - interval '20 days' GROUP BY date_trunc('day',assigndate)

Using CASE, WHEN, THEN, END in a select query with MySQL

谁说胖子不能爱 提交于 2019-12-22 06:59:56
问题 I'm working on a baseball related website. I have a table with a batting lineup for two baseball teams: +----+----------+--------------+--------+ | id | playerId | battingOrder | active | +----+----------+--------------+--------+ Batting order is an integer between 1 and 20. This corresponds to the following logic: Batting Order 1-9 — Away Team Lineup Batting Order 10 — Away Team Pitcher Batting Order 11-19 — Home Team Lineup Batting Order 20 — Home Team Pitcher The active field is a tinyint

Subquery in LINQ that's in the select statement, not the where clause

南笙酒味 提交于 2019-12-22 06:32:51
问题 I need to do something like the following SELECT p.name, (SELECT COUNT(p.id) FROM products WHERE products.parent_id = p.id) AS sub_products FROM products AS p I see lots of LINQ examples of subqueries in the where clause,but nothing like this where it's in the select statement. 回答1: This query should be equivalent: var query = Products.Select(p => new { p.Name, SubProducts = Products.Count(c => c.parent_id == p.id) }); foreach (var item in query) { Console.WriteLine("{0} : {1}", item.Name,