subquery

MySQL: NOT IN with sub select not working as expected?

筅森魡賤 提交于 2020-02-08 11:05:48
问题 This is my query: SELECT customer_email FROM sales_flat_order WHERE customer_email NOT IN (SELECT customer_email FROM sales_flat_order WHERE status != 'holded'); There are 3 rows with status holded for my test customer_email test@example.com, no other status for that mail. For some reason, the full query returns no matches. When I fill the NOT IN manually like that, it works, I get my 3 rows: SELECT customer_email FROM sales_flat_order WHERE customer_email NOT IN ('whatever', 'foobar', '123

MySQL: NOT IN with sub select not working as expected?

戏子无情 提交于 2020-02-08 11:03:12
问题 This is my query: SELECT customer_email FROM sales_flat_order WHERE customer_email NOT IN (SELECT customer_email FROM sales_flat_order WHERE status != 'holded'); There are 3 rows with status holded for my test customer_email test@example.com, no other status for that mail. For some reason, the full query returns no matches. When I fill the NOT IN manually like that, it works, I get my 3 rows: SELECT customer_email FROM sales_flat_order WHERE customer_email NOT IN ('whatever', 'foobar', '123

MySQL: NOT IN with sub select not working as expected?

人走茶凉 提交于 2020-02-08 11:02:07
问题 This is my query: SELECT customer_email FROM sales_flat_order WHERE customer_email NOT IN (SELECT customer_email FROM sales_flat_order WHERE status != 'holded'); There are 3 rows with status holded for my test customer_email test@example.com, no other status for that mail. For some reason, the full query returns no matches. When I fill the NOT IN manually like that, it works, I get my 3 rows: SELECT customer_email FROM sales_flat_order WHERE customer_email NOT IN ('whatever', 'foobar', '123

Selecting multiple columns from a subquery

依然范特西╮ 提交于 2020-02-07 08:20:11
问题 I've searched a lot, but still no chance on having a subquery to return multiple columns all at once. The following code works, but it sucks: SELECT (SELECT Column1 FROM dbo.fnGetItemPath(ib.Id)) AS Col1, (SELECT Column2 FROM dbo.fnGetItemPath(ib.Id)) AS Col2, (SELECT Column3 FROM dbo.fnGetItemPath(ib.Id)) AS Col3 FROM ItemBase ib I actually have got no idea how to pass ib.Id to the function and get the entire Column1, Column2, Column3 columns without calling the fnGetItemPath function 3

Best way to reference an outer query / subquery?

牧云@^-^@ 提交于 2020-02-04 01:24:08
问题 I'm trying to reference a field from the 1st select table in the 3rd select(subquery) table. However, that field isn't recognized when it goes to that sub-level of a query. The php code I'm working on uses sql to return part of the sql command (string) that will be used in other places. I've came up with this example that shows up the kind of nested querys that I want to solve. In here I'm trying to get the name and emails of users that are working at night and have a matching job rank for an

MySQL row subquery comparison issue

别说谁变了你拦得住时间么 提交于 2020-02-03 04:10:00
问题 I have a small mysql table (MySQL version 5.6.23) : +-----------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------+------+-----+---------+----------------+ | id | int(6) unsigned | NO | PRI | NULL | auto_increment | | dividends | float(8,6) | YES | | NULL | | +-----------+-----------------+------+-----+---------+----------------+ My where clause follows the row-subqueries syntax. If I do: SELECT id,

MySQL row subquery comparison issue

早过忘川 提交于 2020-02-03 04:07:04
问题 I have a small mysql table (MySQL version 5.6.23) : +-----------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-----------------+------+-----+---------+----------------+ | id | int(6) unsigned | NO | PRI | NULL | auto_increment | | dividends | float(8,6) | YES | | NULL | | +-----------+-----------------+------+-----+---------+----------------+ My where clause follows the row-subqueries syntax. If I do: SELECT id,

How to use subquery table alias in WHERE clause in MySQL

白昼怎懂夜的黑 提交于 2020-01-30 08:08:48
问题 SELECT invoice_id, sum_amount FROM (SELECT invoice_id, SUM(amount) AS sum_amount FROM invoice GROUP BY invoice_id) AS TEMP_TABLE WHERE sum_amount in (SELECT MAX(sum_amount) FROM TEMP_TABLE); When I tried to use the TEMP_TABLE, an error occurred and said TEMP_TABLE doesn't exist. Why doesn't it work? I think the execution order is "FROM" then "WHERE", the table alias has been created at that time. 回答1: You can't calculate max for the previously calculated alias in sub query alternatively you

How to use subquery table alias in WHERE clause in MySQL

断了今生、忘了曾经 提交于 2020-01-30 08:08:46
问题 SELECT invoice_id, sum_amount FROM (SELECT invoice_id, SUM(amount) AS sum_amount FROM invoice GROUP BY invoice_id) AS TEMP_TABLE WHERE sum_amount in (SELECT MAX(sum_amount) FROM TEMP_TABLE); When I tried to use the TEMP_TABLE, an error occurred and said TEMP_TABLE doesn't exist. Why doesn't it work? I think the execution order is "FROM" then "WHERE", the table alias has been created at that time. 回答1: You can't calculate max for the previously calculated alias in sub query alternatively you

MS SQL Server pivot table with subquery in column clause

坚强是说给别人听的谎言 提交于 2020-01-27 07:46:29
问题 Im sure this is a simple technique although I can't find an answer so far! I have TIMESTAMP | POINTNAME | VALUE 2012-10-10 16:00:00 AHU01 20 2012-10-10 16:00:00 AHU02 25 2012-10-10 16:00:15 AHU01 26 2012-10-10 16:00:15 AHU02 35 etc... ( for approx 800 POINTNAMES) with many pointnames I dont want to list each one in the 'IN' clause of the pivot 'FOR' (as syntax given below) definition but would like to use perhaps a subquery. So what I would like is all the POINTNAME values as columns with A