Questions tagged [in-subquery]

Use this tag for questions related to subqueries (i.e. a query that is nested inside a SELECT, INSERT, etc.), that use IN at their outer statement.

An example of an is:

WHERE th.parent = 1015 IN (
    SELECT DISTINCT(th1.tid)
    ...
);

as demonstrated in MySQL DELETE FROM with subquery as condition.

Note that this tag is general in its scope, i.e. that it is not restricted in SQL questions only, but any question referring to this type of IN subqueries is welcome to use this tag.

128 questions
0
votes
1 answer

MYSQL with multiple SUBQUERIES

Hi and thanks for reading. SELECT DISTINCT (thisID and thisNAME) from table1 WHERE thisID IN (SELECT id from table2 WHERE ... condtions) OR (SELECT id from table3 WHERE ... different condtions) OR (SELECT id from table99 WHERE ...even more…
Will
  • 389
  • 1
  • 11
0
votes
1 answer

Inner left join to get value on previous date

I am trying to get value of GR2 on previous date TabDate in following query: DECLARE @ShN money=STRLOG.dbo.fn_VarValue('ShN') DECLARE @ShNs money=STRLOG.dbo.fn_VarValue('ShNs') DECLARE @ShNN money=STRLOG.dbo.fn_VarValue('ShN') …
litpost
  • 105
  • 3
  • 12
0
votes
1 answer

Use comma delimited string from query in subquery

I would like to use comma delimited ID string as an input into the same query. i.e: Set @IDs := ''; Select @IDs := CAST(GROUP_CONCAT(DISTINCT CAST(`id` as CHAR)) AS Char) AS 'ID String', ( Select FORMAT(Sum(`Quantity`),0) from 'My Table' Where…
0
votes
2 answers

WHERE IN with subquery NOT WORKING as expected

I am trying to get result from my categories table using the parent path i have created. When i launch the request WHERE IN with manual data it's working perfectly. When i am trying the same request dynamically with subquery, i got only one result…
lwillems
  • 103
  • 13
0
votes
1 answer

MySQL nested, nested subquery not getting outter variable

I have a spendings table and a dates table, that are joined by date_id and id... What I'm trying to do, is get from 1 query all the info from spendings, plus the sum of all the spendings but with a limit and/or offset This is the query right…
C Alex
  • 125
  • 1
  • 8
0
votes
1 answer

Doctrine : join on several values in a subquery

I created a query in SQL but I don't know hos to reproduce with doctrine. The difficulties, I would like to join on 2 values in a subquery. Here my SQL query : SELECT u.nom FROM profile p1 JOIN user u ON u.id = p1.id_user JOIN ( SELECT…
0
votes
1 answer

Retrieving a single column in a subquery having a "having" condition

Well, I've got a subquery that contains an "having" condition. So I have to add the parameters in the having condition in the select clause, right? So I've got the subquery: SELECT sfd.id, sfd.StartTime, sfd.EndTime, sd.StartTime FROM…
Christian Graf
  • 406
  • 2
  • 10
  • 26
0
votes
3 answers

Is there a better way to write this SQL than using WHERE ... IN (subquery)?

is there a better way to write this SQL than using WHERE ... IN (subquery)? SELECT device.mac, reseller.name, agent.name FROM device LEFT JOIN global_user ON device.global_user_id = global_user.id LEFT JOIN agent ON global_user.id =…
user267367
0
votes
1 answer

How can I optimize this IN clause in mysql?

I have following table structure: CREATE TABLE listing_attributes ( id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, listing_id INT, attribute_id INT, value LONGTEXT, FOREIGN KEY ( attribute_id ) REFERENCES…
rajnz18
  • 60
  • 5
0
votes
2 answers

Subquery result returning comma separated values using IN clause MySQL

Friends, two tables one table is CREATE TABLE `vbw_push_notifications` ( `push_notification_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary key ,Auto increment field', `push_notification_customer_ids` text NOT NULL COMMENT 'comma…
shanavascet
  • 589
  • 1
  • 4
  • 18
0
votes
3 answers

TSQL error when CASE-WHEN returns subquery

Is there a way to nest CASE-WHEN statements in an IN() statement such that one of the WHEN or ELSE returns a subquery. To me, it should not be an issue, but somehow I am getting error: "Subquery returned more than 1 value." IN() is supposed to…
Aamir
  • 791
  • 3
  • 15
  • 28
0
votes
1 answer

Soundex comparison with comma separated subquery?

I am performing a soundex query on a table of users. A subquery of users is provided as a comma separated list of strings. I want to do something akin to the following, but I cannot find the write syntax to make this work. select * from ((Select…
The Prophet
  • 338
  • 5
  • 11
0
votes
2 answers

Selecting the maximum value for each ID on mysql 3.23

The whole issue is due to the fact that sub queries in a IN clause doesn't seem to work on mysql 3.23. Here's my table structure for table1: |**idTable1**|**strDescription**| |1 |blablabla | |2 |blablabla …
Simon
  • 364
  • 1
  • 5
  • 18
0
votes
1 answer

Filtering out results using sub-queries in ReQL/Rethink?

I'm am trying to learn more about RethinkDB and its sub-query abilities. I was wondering if the following would be possible in ReThinkDB: // example of "post" document: { id: .., allow: [], disallow: [some_label_id, other_label_id], ... } //…
dgo.a
  • 2,634
  • 23
  • 35
0
votes
2 answers

Need to optimize not in sql statement

I have an sql query: select person from table t1 inner join person_history ph on t1.person = ph.person and t1.person not in (select person from person_history where effective_date < '01-01-2013') and ph.person.effective_date >…
1 2 3
8
9