473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Reference to Table Alias in From Clause to be Used by Subquery inSelect Clause

I am using 5.0.26-NT on Windows 2000.

I have need to use a reference in the outer from clause in a subquery in
the select clause. Consider the following example:

Select (select b.baitID from b where b.entrydate curdate()) as
wantedBaitIDs from bait_tbl b;

My actual need is more complex than this as part of it is a rough cross
tab. If I try to define the table in the alias, not only do I lose
whatever benefits there are in the particular join I would use in the
outer from clause but would also require the join to be defined in each
subquery, requiring it to be examined each time it is used.

To be absolutely clear, in this example I want to use bait_tbl with the
alias of b in the subquery. In my actual query I reference the same
table twice with a different join set for each. I need to reference a
particular alias as that has the join set I need.
Oct 12 '06 #1
5 10129
No bother wrote:
I am using 5.0.26-NT on Windows 2000.

I have need to use a reference in the outer from clause in a subquery in
the select clause. Consider the following example:

Select (select b.baitID from b where b.entrydate curdate()) as
wantedBaitIDs from bait_tbl b;

My actual need is more complex than this as part of it is a rough cross
tab. If I try to define the table in the alias, not only do I lose
whatever benefits there are in the particular join I would use in the
outer from clause but would also require the join to be defined in each
subquery, requiring it to be examined each time it is used.

To be absolutely clear, in this example I want to use bait_tbl with the
alias of b in the subquery. In my actual query I reference the same
table twice with a different join set for each. I need to reference a
particular alias as that has the join set I need.
I just realized that I was going about the whole thing wrong, though the
answer to this question might still prove useful to someone else, or
possibly me in the future.

Insufficient sleep + insufficient caffeine -inappropriate posts to
technical newsgroups.

I beg the indulgences of the community here.
Oct 12 '06 #2
No bother wrote:
Select (select b.baitID from b where b.entrydate curdate()) as
wantedBaitIDs from bait_tbl b;
Does something like this do what you want?

SELECT IF(b.entrydate curdate(), b.baitID, NULL) AS wantedBaitIDs
FROM bait_tbl AS b;

Can you be more specific about the more complex case? It's hard to
visualize what you're describing unless we know the join conditions, etc.
Regards,
Bill K.
Oct 12 '06 #3
Bill Karwin wrote:
No bother wrote:
>Select (select b.baitID from b where b.entrydate curdate()) as
wantedBaitIDs from bait_tbl b;

Does something like this do what you want?

SELECT IF(b.entrydate curdate(), b.baitID, NULL) AS wantedBaitIDs
FROM bait_tbl AS b;

Can you be more specific about the more complex case? It's hard to
visualize what you're describing unless we know the join conditions, etc.
Regards,
Bill K.
No, I specifically thought I needed to use a subquery.

Select
max(if(b.baittype = 'Worms',(select count(b.baitID) where b.entrydate <
curdate(),0) as 'Worms',
max(if(b.baittype = 'Lure', (select count(b.baitID) where b.entrydate >
curdate(),0) as 'Lure',
max(if(b.baittype = 'Other',(select count(b.baitID) where
b.baitDistributor = 'A Retailor Near You'),0) as 'Other'
from bait_tbl b inner join customer_tbl c on b.baitID = c.PreferedBaitID

As you can see the specific criteria to be used depends on the baittype.

All I really want to know is if I can refer to an alias so designated in
the outmost from clause from within a subquery in the select clause. In
the first example I gave the alias is defined outside of the subquery.
Mysql does not recognize that the table alias in the subquery means the
table outside of the subquery.

Oct 12 '06 #4
No bother wrote:
All I really want to know is if I can refer to an alias so designated in
the outmost from clause from within a subquery in the select clause.
Yes you can, it's called a correlated subquery when you do that.

But you can't do what you're doing, which is a SELECT with a COUNT() and
a WHERE clause, but no FROM clause.

Maybe this will help: the alias b is not an alias for the whole table
bait_tbl. It's an alias to each individual row in bait_tbl. So during
a given expression evaluation, b refers to only one row. You can
compare columns of that one row to corresponding columns queried in a
subquery, and they'll be compared row by row.

I wish I could suggest something more substantial, but I still can't
tell from your example what you want the output to be.

Regards,
Bill K.
Oct 12 '06 #5
Bill Karwin wrote:
No bother wrote:
>All I really want to know is if I can refer to an alias so designated
in the outmost from clause from within a subquery in the select clause.

Yes you can, it's called a correlated subquery when you do that.

But you can't do what you're doing, which is a SELECT with a COUNT() and
a WHERE clause, but no FROM clause.

Maybe this will help: the alias b is not an alias for the whole table
bait_tbl. It's an alias to each individual row in bait_tbl. So during
a given expression evaluation, b refers to only one row. You can
compare columns of that one row to corresponding columns queried in a
subquery, and they'll be compared row by row.

I wish I could suggest something more substantial, but I still can't
tell from your example what you want the output to be.

Regards,
Bill K.
I was hoping you wouldn't say that. I think I understand now.
Oct 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jack Tanner | last post by:
I have two complex subqueries that I need to join. I suspect this problem is due to using aliases instead of table names, but I don't know how to work around it (temporary tables?). Please help. ...
5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
14
by: John | last post by:
Hi all, I am doing the change from having worked in Oracle for a long time to MS SQL server and am frustrated with a couple of simple SQL stmt's. Or at least they have always been easy. The...
3
by: John Baker | last post by:
Hi: I have developed a series of complex queries against a table named "timesheetsnew". These queries work fine. I now wish to apply exactly the same queries to an identically formatted table...
0
by: Roman S. Golubin | last post by:
Hi, I found strange problem, when I use SQL server via proxy to sqlxml webservice... I create procedure: ---------- CREATE PROCEDURE TestConnection AS
5
by: Richard Dixson | last post by:
I created a new C# web application. Basically all I am trying to do is create a table that consists of a few rows with two columns in each. And then to set those columns to text values from my...
3
by: Rodríguez Rodríguez, Pere | last post by:
Hello, I think I have found a query problem when the query has an alias for a table and use alias item and table name. I ilustrate the problem with a simple table and query. prr=# create...
2
by: tom1234 | last post by:
Hello I am trying to write an SQL statement that uses two derived tables / subqueries in a following way: SELECT * FROM tbl A ... INNER JOIN (SELECT * FROM tbl2 WHERE col...
1
by: Lanaa | last post by:
I have a problem joining two tables. mysql> SELECT VARA.VID, VARA.ANTAL, VARA.PRIS, LEVERANTOR.LEVID, LEVERANTOR.TELEFONNUMMER FROM (VARA INNER JOIN LEVERANTOR ON VARA.VID=LEVERANTOR.LEVID) INNER...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.