subquery

How to merge these queries into 1 using subquery

こ雲淡風輕ζ 提交于 2019-12-13 09:51:49
问题 Select * from HotelPerson Where RoomID IN (select ID from HotelRoom Where BookingID = 36 ) Select * from HotelCancelationPolicy Where RoomID IN (select ID from HotelRoom Where BookingID = 36 ) How can I merge these both queries into 1 query ? 回答1: This will give you all the columns from both tables in one table. SELECT * FROM HotelPerson A, HotelCancelationPolicy B WHERE A.RoomID = B.RoomID AND A.RoomID IN (SELECT ID FROM HotelRoom WHERE BookingID = 36) 回答2: Use UNION to Get Distinct elements

UNION two tables in a subquery

情到浓时终转凉″ 提交于 2019-12-13 08:25:42
问题 I am trying to UNION two tables in a subquery, but apparently this is not allowed? A simplified version of my query is: SELECT * FROM user, organization WHERE user.id NOT IN ( (SELECT distinct user_id FROM web_request) UNION (SELECT distinct user_id from user_milestone) ) AND user.organization_id = organization.id My question is this: What is the best way to get around this shortcoming of MySQL? 回答1: SELECT u.*, o.* FROM `user` u INNER JOIN organization o ON u.organization_id = o.id WHERE u

How to get total number of rows within subquery for multiple conditions

对着背影说爱祢 提交于 2019-12-13 07:11:50
问题 This query return number of rows where Sum of Product's Quantity is equal to 0 and Product Name is 'XYZ'. But I want same result for each products. For example if I remove product name from where clause so it will bring SUM of every product where quantity is 0. I want it to return separately for each products. Products may increase in future. I have to first check how many distinct products are there in CustomerProduct table and then need to query same things for each product. How can I do

Adding subquery to a grid query

蹲街弑〆低调 提交于 2019-12-13 07:04:12
问题 Following from this question, I have another query that I need to subtract a value of 10 from for all negative numbers in the data. Sadly, I'm just not sure how to implement the same subquery as is given in the previous question. The query in question is SELECT 10 * (c.customer_x / 10), 10 * (c.customer_y / 10), COUNT(*) as num_orders, SUM(o.order_total) FROM t_customer c JOIN t_order o ON c.customer_id = o.customer_id GROUP BY c.customer_x / 10, c.customer_y / 10 ORDER BY SUM(o.order_total)

SQL Server: Combine multiple rows into one row

寵の児 提交于 2019-12-13 06:59:09
问题 I've looked at a few other similar questions, but none of them fits the particular situation I find myself in. I am a relative beginner at SQL. I am writing a query to create a report. I have read-only access to this DB. I am trying to combine three rows into one row. Any method that only requires read access will work. That being said, the three rows I have, were obtained by a very long sub-query. Here is the outer shell: SELECT Availability, Start_Date, End_Date FROM ( -- long subquery goes

Bad (correlated) or Good (not correlated) subquery?

馋奶兔 提交于 2019-12-13 06:35:16
问题 I wonder, if the subquery in the following update statement is a good (not correlated) or bad (subquery)? In other words, my question is, is it an inefficient query? UPDATE tableA SET field1=0 FROM tableA WHERE field2 IN (SELECT field2 FROM tableA WHERE someField IS NOT NULL AND someOtherField = 'ABC') 回答1: Your query is not correlated ,its just a subquery.. below is a correlated subquery.. UPDATE a SET field1=0 FROM tableA a WHERE exists (SELECT 1 FROM tableB b WHERE a.somecol=b.somecol) one

is there better way to do these mysql queries?

让人想犯罪 __ 提交于 2019-12-13 04:42:54
问题 Currently i have three different tables and three different queries, which are very similiar to each others with almost same joins. I was trying to combine all that three queries with in one query, so far not much success though. I will be very happy if someone has better solution or a direction to point. Thanks. 0.0013 SELECT `ilan_genel`.`id`, `ilan_genel`.`durum`, `ilan_genel`.`kategori`, `ilan_genel`.`tip`, `ilan_genel`.`ozellik`, `ilan_genel`.`m2`, `ilan_genel`.`fiyat`, `ilan_genel`.

php Codeigniter - error while executing library Subquery.php

ぐ巨炮叔叔 提交于 2019-12-13 04:41:57
问题 I have been using a library for subqueries to work - Subquery.php Ref : https://github.com/NTICompass/CodeIgniter-Subqueries $this->db->select('test'); $this->db->select('test2'); $this->db->from('table'); $sub = $this->subquery->start_subquery('where_in'); $sub->select('IDs'); $sub->from('idTable'); $sub->where('date', '2011-07-10'); $this->subquery->end_subquery('id'); I think that this statement: $sub = $this->subquery->start_subquery('where_in'); contains the error. When I execute this

Update a table that I am sub-selecting from

◇◆丶佛笑我妖孽 提交于 2019-12-13 04:18:06
问题 Why do I get the following error when I try to update the a_fees table? From what I can gather, you can not base the sub-selection criteria on a table that is being updated? Is it because SQL is read backwards? How can I get around this? Error Message: 1093 - You can't specify target table 'a_fees' for update in FROM clause UPDATE a_fees SET active = 'N' WHERE a_fees.fee_id IN ( SELECT fee_id FROM a_fees WHERE verified = 'N' HAVING audit_fees + audit_related_fees + tax_fees + other_fees <

PostgreSQL ERROR: subquery in FROM cannot refer to other relations of same query level

送分小仙女□ 提交于 2019-12-13 04:15:19
问题 I'm having an inordinate amount of trouble using CTEs as arguments to a PostgreSQL function call, and no amount of refactoring into subqueries seems to help; I get either subquery in FROM cannot refer to other relations of same query level or function expression in FROM cannot refer to other relations of same query level . The simplified SQL: create type similarity as ( distance float, explanation text ); create or replace function similarity(my_user_id int) returns table(user_id int,