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
Related
I was wondering if there's any way to add a subquery with a switch case to the form clause of my select query in order to select a table based on a condition.
For example:
select a.*
from (select (case when (table2.column = 'something')
then (table2.tablename1)
else (table2.tablename2)) as tablename
from table2
where table2.column2 = 'blabla'
limit 1
) a
I tried to write that in many variation & so far non of them worked.
On the most successful tryouts (when I got no mysql errors) it returned the name of the table as the result itself (for example: the value that's in table2.tablename2). I understand why it did that (because I selected everything from a select results...) but how can I use the tablename from the results in order to set the table on the main query?
Hope that make sense...
Any idea?
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.
I'm using PDO with MySQL.
I want to select all rows from a given table with distinct values in a given column, but SELECT DISTINCT column_name FROM table returns the rows with only that column_name. Therefore I can't access the other row's columns.
I've been searching for answers and it looks like SELECT DISTINCT column_name FROM table is supposed to return all the rows with distinct values inside column_name with all the row's columns. However, I only get the column I want distinc'ed:
Array
(
[image] => leather_helmet.jpg
// there are supposed to be more fields here...
)
May this be a PDO's bug or am I doing something wrong?
Thanks in advance! :)
If you want only 1 column distinct you have to think of which record you want for the other columns. For instance if you like the min id record for the distinct column then you can do
SELECT *
FROM armor_unsealed
WHERE id IN
(
SELECT min(id)
FROM armor_unsealed
WHERE piece=:piece
GROUP BY image
)'
I am trying to count line that has columns which been repeated (their value) more than x times in mysql, every time I try to execute this query I get an error :
SELECT DISTINCT
idLogin, count(idLogin ) AS "cou"
FROM
`products` AS t
GROUP BY
idLogin
HAVING
t.cou > 2
why this error is happening
Use this one
SELECT DISTINCT idLogin, count(idLogin ) AS cou FROM `products`
GROUP BY idLogin HAVING cou > 2
you can put that expression count(idLogin ) directly in having clouse....
it will not give any error.. and will be more understable as well....
or else you can do one thing -
select idLogin,cou from (
SELECT DISTINCT idLogin, count(idLogin ) AS cou
FROM products
GROUP BY idLogin ) t
where t.cou >2
Remove the double quotes from the aliased column name.
MySQL uses single back-quotes for escaping table and column names, not double quotes.
The correlation name t does not have a column cou. Just use this:
HAVING cou > 2
PS: DISTINCT is redundant in your query. The GROUP BY ensures there will only be one row per distinct value of idLogin.
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