subquery

Pending friend request query for friends table and grabbing data from other tables

為{幸葍}努か 提交于 2019-12-11 03:37:19
问题 To get pending request for selected user I use: SELECT f1.asker_user_id AS friend_id FROM friends AS f1 LEFT JOIN friends AS f2 ON f1.asked_user_id = f2.asker_user_id AND f1.asker_user_id = f2.asked_user_id WHERE f1.status = 1 AND f2.status IS NULL AND f1.asked_user_id = 2 http://www.sqlfiddle.com/#!2/0f7a0d5/65 It correctly returns user 3. VALUES (3,2,1) But I'd like to get more data about this user 3. I need name_surname and avatar from users table. And all word_id s of this user from

Mysql Nested sub queries unknown column error

倖福魔咒の 提交于 2019-12-11 02:50:58
问题 i have a pretty big query with 2 sub queries. My big query updates an entire column E. The table looks like this: -------------------------- id | A | B | E | -------------------------- 1 | NULL | NULL |NULL| -------------------------- 2 | 4 | 6 |NULL| -------------------------- 3 | 6 | 9 |NULL| -------------------------- This is my entire query below: Update mydatabase.mytable , (SELECT t1.A/AVG(t2.A) FROM mytable t1 JOIN mytable t2 ON t2.id <= t1.id group by t1.id ) AS C, (SELECT t1.B/AVG(t2

Linq to Entities with Subselect in select part

こ雲淡風輕ζ 提交于 2019-12-11 02:39:06
问题 We are starting with a MVC project using EF. We will need write a lot of queries in LINQ using subselect and don't have figured out yet how this could be done. The most simple case of these is in this form: select p.Id, p.Title, (select count(*) from Comments c where c.PostId = p.Id ) as CommentCount from Post p where p.UserId = 'John'; Reading the "101 page" of examples from Microsoft and Stack Overflow I couldn't find a example like this. I found examples using join and group, but in some

Nested queries to get count with two conditions

江枫思渺然 提交于 2019-12-11 02:29:45
问题 I have these 3 tables, tbl_1- ip |isp |infection ---------------------- 1 |aaaa |malware 2 |bbbb |malware 3 |cccc |ddos 3 |cccc |trojan 4 |dddd |ddos tbl_2- ip |isp |infection ---------------------- 1 |aaaa |malware 3 |cccc |ddos 4 |dddd |trojan 5 |eeee |trojan 6 |ffff |other tbl_3- ip |isp |infection ---------------------- 1 |aaaa |ddos 6 |ffff | 2 |bbbb |other Which i need to get the result as follows, result- ip |isp |infection |ipCount |ispCount |infectionCount ---------------------------

Select List of Column Names / Aliases from Custom Sub-Query

淺唱寂寞╮ 提交于 2019-12-11 02:27:19
问题 In Oracle, is there a way to select all the columns that are returned from a custom query with aliases? As an example, let's say we have a query as the following: SELECT FIRST_NAME AS COL1, LAST_NAME AS COL2, ADDRESS AS COL3 FROM PEOPLE I would like to know if an encapsulating query can be made that would return: COL1 COL2 COL3 回答1: Here is how to do it in PL/SQL. Don't know if it is possible with straight oracle SQL only. You could always encapulate it in some kind of function if needed.

Tag based SQL query

蹲街弑〆低调 提交于 2019-12-11 02:26:50
问题 Its been a while since I have done any SQL and I am not sure if this problem has an easy solution or not. Also I am a bit of a Noob. I am trying to put together an image gallery that allows users to use tags in order to search for images and then click on additional tags to refine the search and lower the number of results but I am having a big issue with the queries involved. This is a simplified version of my current database structure: ( 2 tables with an additional Many-to-Many link table

Using sub-query to SELECT MAX value along with LEFT JOIN

余生颓废 提交于 2019-12-11 02:12:40
问题 I have a query for getting search results, which works fine. Example of successful query: SELECT individuals.individual_id, individuals.unique_id, TIMESTAMPDIFF(YEAR,individuals.day_of_birth,CURDATE()) AS age, individuals_dynamics.id, individuals_achievements.degree FROM individuals as individuals LEFT JOIN individuals_dynamics AS individuals_dynamics ON individuals.unique_id = individuals_dynamics.individual_id LEFT JOIN individuals_achievements AS individuals_achievements ON individuals

Update statement error: Subquery returned more than 1 value

心不动则不痛 提交于 2019-12-11 01:09:02
问题 I am trying to update all records in a column so that they start with 'CD' e.g. DCE206 would become CDE206. UPDATE table SET column = REPLACE(column1, 'DC', 'CD') WHERE column1 LIKE 'DC%' I am using the above update statement however the following error appears 'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.' Is there anything I can change in the statement to make this happen or do I need

Understanding a basic SQL query

笑着哭i 提交于 2019-12-11 01:06:00
问题 I have a query like SELECT tran_number FROM table_a WHERE customer_id IN (SELECT customer_id FROM table_b WHERE customer_key = 89564 AND ( other_phn_area_code || other_phnum_pfx_num || other_phnum_sfx_num IN (123456789))) AND phn_area_code || phnum_pfx_num || phnum_sfx_num IN (123456789) The above code is working fine. The concern is with the inner query (copied inner query alone below)... (SELECT customer_id FROM table_b WHERE customer_key = 89564 AND ( other_phn_area_code || other_phnum_pfx

Moving average query MS Access

偶尔善良 提交于 2019-12-11 00:55:27
问题 I am trying to calculate the moving average of my data. I have googled and found many examples on this site and others but am still stumped. I need to calculate the average of the previous 5 flow for the record selected for the specific product. My Table looks like the following: TMDT Prod Flow 8/21/2017 12:01:00 AM A 100 8/20/2017 11:30:45 PM A 150 8/20/2017 10:00:15 PM A 200 8/19/2017 5:00:00 AM B 600 8/17/2017 12:00:00 AM A 300 8/16/2017 11:00:00 AM A 200 8/15/2017 10:00:31 AM A 50 I have