In the following query:
SELECT 1 FROM (SELECT pass
FROM table_name
WHERE ssid=?) b
WHERE pass=?
what does ssid) b WHERE pass=?; actually mean in the original query?
SELECT 1 FROM (SELECT pass FROM table_name WHERE ssid=?) b WHERE pass=?
same as:
SELECT 1 FROM (SELECT pass FROM table_name WHERE ssid=?) AS b WHERE pass=?
b is alias name for subquery. Then in your outer query you can refer to columns like:
SELECT b.pass FROM (SELECT pass FROM table_name WHERE ssid=?) AS b
See manual
Subqueries are legal in a SELECT statement's FROM clause. The actual
syntax is:
SELECT ... FROM (subquery) [AS] name ...
The [AS] name clause is mandatory, because every table in a FROM
clause must have a name. Any columns in the subquery select list must
have unique names.
It's simply being used as an identifier for the nested selection statement. It's effectively assigning the results of that query to an imaginary table named 'b', so you can treat that nested statement much like you would a normal table.
Related
create table t1 (c1 integer);
select c1*3 temp, case when (temp <>3) then 1 else 0 end from t1;
Query fails in both Oracle and MySQL But why they doesn't support this type of queries?
I already went through the answers related to column alias. Everywhere its explained what is supported and what is not and reasoning about why not in where clause. But question is about why its not supported in select clause.
An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column:
SELECT SQRT(a*b) AS root FROM tbl_name
GROUP BY root HAVING root > 0;
SELECT id, COUNT(*) AS cnt FROM tbl_name
GROUP BY id HAVING cnt > 0;
SELECT id AS 'Customer identity' FROM tbl_name;
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. For example, the following query is illegal:
SELECT id, COUNT(*) AS cnt FROM tbl_name
WHERE cnt > 0 GROUP BY id;
The WHERE clause determines which rows should be included in the GROUP BY clause, but it refers to the alias of a column value that is not known until after the rows have been selected, and grouped by the GROUP BY.
In the select list of a query, a quoted column alias can be specified using identifier or string quoting characters:
SELECT 1 AS `one`, 2 AS 'two';
Elsewhere in the statement, quoted references to the alias must use identifier quoting or the reference is treated as a string literal. For example, this statement groups by the values in column id, referenced using the alias a:
SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name
GROUP BY `a`;
But this statement groups by the literal string 'a' and will not work as expected:
SELECT id AS 'a', COUNT(*) AS cnt FROM tbl_name
GROUP BY 'a';
Source: https://docs.oracle.com/cd/E17952_01/refman-5.0-en/problems-with-alias.html
You can't use aliases in any sections except ORDER BY (in Oracle)
So you can do either:
select c1*3 temp, case when (c1*3 <>3) then 1 else 0 end from t1;
Or:
select temp, case when (temp <>3) then 1 else 0 end
from (
select c1*3 temp from t1);
SELECT b.*,
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1
From this sql syntax, what is "," which after "*" mean?
and anyone can explain this query to me or tell me where I can start?
It means all the columns from table Booking, and to the far right (the last column per row) bring in the user_name column from table user relating on the user.user_id matching the booking.booking_inspector. Such that the Booking.confirm is 1, and Booking_inspector is filled in with a parameter passed.
So it limits the rows of output to confirm is 1 and Booking_Inspector is the parameter passed (or bound, etc) depending on the language calling it.
Select * means all columns. So all columns from the one table, and just one column from the other
In this case, (select a.USER_NAME from A.db.USER a where a.USER_ID=b.Booking_Inspector) is the subquery which will return column a.USER_NAME. So this query is selecting everything from b (b.*) and the column a.USER_NAME from the subquery. So like you put comma between column names in select query, it is same.
select all columns from b and one more column from that subquery as USER_NAME.
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
That whole thing above as 1 column
SELECT b.*, [USER_NAME]
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1
I have a question on HAVING statement in SQL.
Is a query like this possible?
SELECT COUNT(T.IDtif) AS NumeroTifosi
FROM Tifosi T, Partita P
WHERE T.IDtif=P.Tifoso AND P.Partita=P.Idpar
HAVING COUNT(P.Idpar) = ( SELECT COUNT(Idpar) FROM Partita
WHERE Data BETWEEN “2002101” AND ”20021231”)
I don't understand if it is possible to compare an aggregation function, in a HAVING statement, with a subquery that returns a single value.
That's what the parentheses do. You can compare a single result of a SELECT by turning telling SQL "it's ok, this has one value".
It could be:
SELECT ColA = (SELECT ColB from TableB Where Id = 1)
FROM TableA
or
SELECT *
FROM TableA
WHERE ColA = (SELECT ColB from TableB Where Id = 1)
or even with HAVING as you describe.
Yes. This query if perfectly valid. Are you getting an error when you try to run it?
What you are doing is running a subquery once to return a count. Then for each group, you are checking if the number of rows in that group is equal to the number returned by the subquery.
I have a simple query that selects one field and only one row, thus one value.
Is there any way to make it return NULL if the query results in an empty set? Instead of returning zero rows?
I think I need to use something with NOT EXISTS, THEN NULL but not certain about it.
select
(Your entire current Select statement goes here) as Alias
from
dual
dual is a built in table with a single row that can be used for purposes like this. In Oracle this is mandatory. MySQL supports it, but you can also just select a single value without specifying a table, like so:
select
(Your entire current Select statement goes here) as Alias
In either case you're selecting a single value. This means that:
If your select returns one value, that value is returned.
If your select statement returns one column, but no rows, NULL will be returned.
If your select statement returns multiple columns and/or multiple rows, this won't work and the query fails.
An easy way to do this is with aggregation:
select max(col)
from t
where <your condition here>
This always returns one row. If there is no match, it returns NULL.
Late reply but I think this is the easiest method:
SELECT
IFNULL((SELECT your query), NULL)
Use a UNION with a NOT EXISTS(original where clause)
select col1
from mytable
where <some condition>
union
select null
where not exists (
select * from mytable
where <some condition>)
You can use COALESCE for example:
SELECT COALESCE(Field1,NULL) AS Field1 FROM Table1
Edit 1:
sorry i mistake with return field as null not result set,for result set return as null use Union and Exist Function like this:
SELECT NULL AS Field1 FROM Table1 WHERE not EXISTS(SELECT Field1 FROM Table1 WHERE Field2>0)
UNION
SELECT Field1 FROM Table1 WHERE Field2>0
I am not a MySQL expert by any means, but after reading the documentation for the SELECT statement, I did not find an answer to my problem.
I have this statement:
SELECT COUNT(*)=x.c FROM someTable,
(SELECT COUNT(*) c
FROM someTable
WHERE firstId <= secondId) x;
And I'm trying to figure out what the x.c means in the context of the query? Specifically, what is with the x that seems to be hanging out there?
I interpret the nested SELECT as SELECT COUNT(*) as c, making an alias for the row count as c, is that what the x is as well? What would it be an alias for?
Thanks!
The x is a table alias - a name for the nested SELECT statement in parentheses.
COUNT(*)=x.c
Is a boolean condition that the total row count of someTable be equal to the row count of someTable where firstId <= secondId
x.c is the column name for the count returned by the subquery.
They name the subquery "x" and in the subquery they name the count(*) "c". So "x.c" means the count returned by the subquery.
The x is an alias to the subquery - you will note that there is an x just after the subquery, denoting the alias name for it.
x is an alias for the table (SELECT COUNT(*) c FROM someTable WHERE firstId <= secondId).
MySQL requires that table subqueries have a unique alias. You'll notice there's an x at the end of the subquery, which makes the sub queries results appear as coming from table x.
In this case, x.c in the outer query means to refer to field c (the count result) in the aliased table x, which is the subquery.
X is an alias for joined someTable and the select you joined someTable with. I guess so :D
I guessed wrong, guys over me are right :P