473,785 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Easy(?) SQL question

I have two tables

table1:
id state
-----------------
10 washington
11 oregon
12 california

table2:
product from to
--------------------------
piano 10 11
guitar 12 10
piano 12 11

and now I want to do a select-case that returns
-------------
piano washington oregon
piano california oregon

but I can't find out how. I have tried

SELECT id, state
FROM table1, table2
WHERE table2.product = 'piano' and table2.from = table1.state
but I cant figure out how to get the "to" state as well!

Best Regards
Eric

Mar 13 '06 #1
2 1179
<e_********@hot mail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
I have two tables

table1:
id state
-----------------
10 washington
11 oregon
12 california

table2:
product from to
--------------------------
piano 10 11
guitar 12 10
piano 12 11

and now I want to do a select-case that returns
-------------
piano washington oregon
piano california oregon

but I can't find out how. I have tried

SELECT id, state
FROM table1, table2
WHERE table2.product = 'piano' and table2.from = table1.state
but I cant figure out how to get the "to" state as well!

Best Regards
Eric

Not the only way, but you could do this:

SELECT P.Product,
S1.State AS FromState,
S2.State AS ToState
FROM
(table2 AS P INNER JOIN
table1 AS S1 ON P.from=S1.id)
INNER JOIN table1 AS S2 ON P.to=S2.id
WHERE P.product='pian o'

Of course, if the from and to columns in table2 might be missing, then
replace INNER JOIN with LEFT JOIN. Also you should avoid reserved words
(such as 'from') for your field names.

Mar 13 '06 #2

<e_********@hot mail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
I have two tables

table1:
id state
-----------------
10 washington
11 oregon
12 california

table2:
product from to
--------------------------
piano 10 11
guitar 12 10
piano 12 11

and now I want to do a select-case that returns
-------------
piano washington oregon
piano california oregon

but I can't find out how. I have tried

SELECT id, state
FROM table1, table2
WHERE table2.product = 'piano' and table2.from = table1.state
but I cant figure out how to get the "to" state as well!

Best Regards
Eric

Not the only way, but you could do this:

SELECT P.Product,
S1.State AS FromState,
S2.State AS ToState
FROM
(table2 AS P INNER JOIN
table1 AS S1 ON P.from=S1.id)
INNER JOIN table1 AS S2 ON P.to=S2.id
WHERE P.product='pian o'

Of course, if the from and to columns in table2 might be missing, then
replace INNER JOIN with LEFT JOIN. Also you should avoid reserved words
(such as 'from') for your field names.
Mar 13 '06 #3

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

Similar topics

4
1924
by: Derek Wickersham | last post by:
Easy newbie question. I've searched all the PHP documentation I can find, but can't find an answer. What does the "@" mean, in this context: @mysql_select_db( "scripts");
6
1363
by: ben3003 | last post by:
I am only asking for my code to do a simple thing but for some reason i just can't get it to do what i want it to do... it been bugging me all day! The user inputs 2 dates on one page. in the form xx/xx/xxxx I then want the output page to display the amount of days that there are between these two dates, inclusive. For example: 01/01/2005 03/01/2005
1
1608
by: Shelly | last post by:
Here is a question that is probably very easy but that I don't know how to do . I have a web site www.aaa.net. A directory structure has been created for me on the server as and it appears to me as /var/www/html. That is where all my code is placed. I want to have another site, www.aaa.org. I want the directory structure for the html and php for this to be under the www.aaa.net directory tree in a subdirectory org ...
0
1373
by: | last post by:
I don't know SQL at all, but I have a problem now because I must use SQL in my PHP scripts. So please help me! I have 2 tables: id | name ----------- 1 | thing1 2 | thing2 3 | thing3 4 | thing4
13
1483
by: Ghislain Tanguay | last post by:
I have a compiled vb.net app and I want to give the user a choice to launch it from the start line command and pass it a parameter or not. How can I do that in my code? Is it possible? Ex. : MyApp.exe "Go"
3
1216
by: Jeff Calico | last post by:
Hi I have what I think is a easy question about how to pull cross reference data when I transform my XML file. I can't seem to find a good example of how to do this on the web, though. Here is the XML file I am supposed to process:
8
6615
by: John | last post by:
Is there any special code I have to write to log event to Security Event Log? The following code give me "Very Easy Question, How to write log to SECURTY Event Log? Please help" Error // Create an EventLog instance and assign its source. EventLog myLog = new EventLog(); myLog.Source = "Security"; // Write an informational entry to the event log. myLog.WriteEntry("Writing warning to event log.",
6
2298
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double (*f)(float)" But I want to be able to pass a
4
1914
by: robert.b.walden | last post by:
Hello, Easy question from a newbie. I have text boxes which collect users input and on the click of a button do some calculations and output to another text box. I need to easily convert the numbers inputted to int's so I can do the calculations. Also I need to figure out how to pop up an error box if the charactors entered in those boxes are anything but numbers. Thanks in advance for helping someone trying to learn this robust...
7
2453
by: SteveM | last post by:
I am sure this is an easy question, but being relatively new to ASP.NET programming, I can not quite grasp what I need to accomplish what I need to do. What I have is a word document that is rendered as a page (or actualy a part of a page) that is editiable. To do this I let the user download the document, edit it and then upload it back to the site. Then at Page_Open, I convert the .doc file to an html and render it back to the page......
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.