472,791 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 10095
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.