473,387 Members | 1,705 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,387 software developers and data experts.

Plz, what is the most "correct" method

Hi,

Given 2 tables:

Table1:
id Auto,int,PrimKey
table2id int
txt nvarchar50

Table2:
id Auto,int,PrimKey
order int

The scenario: Table2.order defines how the table1.txt is should be ordered.
Table1.table2id contains the id of the order. This cannot be changed :(

How do I select all Table1.txt values but ordered by their corresponding
values of the table2.order field?

--
Thx,
PipHans
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 01-09-2003
Jul 20 '05 #1
7 1646
Hi

The following should do what you require:

SELECT T1.id, T1.txt
FROM Table1 T1 JOIN Table2 T2 ON T1.Table2Id = T2.id
ORDER BY T2.order
John

"PipHans" <pi*****@hotmail.co> wrote in message
news:3f***********************@dtext02.news.tele.d k...
Hi,

Given 2 tables:

Table1:
id Auto,int,PrimKey
table2id int
txt nvarchar50

Table2:
id Auto,int,PrimKey
order int

The scenario: Table2.order defines how the table1.txt is should be ordered. Table1.table2id contains the id of the order. This cannot be changed :(

How do I select all Table1.txt values but ordered by their corresponding
values of the table2.order field?

--
Thx,
PipHans
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 01-09-2003

Jul 20 '05 #2
John Bell wrote:
SELECT T1.id, T1.txt
FROM Table1 T1 JOIN Table2 T2 ON T1.Table2Id = T2.id
ORDER BY T2.order


It complains about the from-clause being wrong.
If I add "LEFT JOIN" instead of "JOIN", it runs like it should. (thx btw :)

Can you explain why it didnt run with just "join"?

--
Pip
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 01-09-2003
Jul 20 '05 #3
Hi

A LEFT JOIN would be all rows in the left hand side table (Table1) with any
matching rows in the right hand table (Table2) therefore an entry in Table2
does not have to exist. If an entry in Table2 does not exist then NULL
values are returned.

JOIN (or INNER JOIN) is where both tables have to contain the joined data.

As you didn't post the offending query or the actual error message, the only
thing I can imaging is some other syntax error:

SELECT T1.id, T1.txt
FROM Table1 T1 LEFT JOIN Table2 T2 ON T1.Table2Id = T2.id
ORDER BY T2.order

Should work OK.

John
"PipHans" <pi*****@hotmail.co> wrote in message
news:3f***********************@dtext02.news.tele.d k...
John Bell wrote:
SELECT T1.id, T1.txt
FROM Table1 T1 JOIN Table2 T2 ON T1.Table2Id = T2.id
ORDER BY T2.order
It complains about the from-clause being wrong.
If I add "LEFT JOIN" instead of "JOIN", it runs like it should. (thx btw

:)
Can you explain why it didnt run with just "join"?

--
Pip
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 01-09-2003

Jul 20 '05 #4
PipHans (pi*****@hotmail.co) writes:
John Bell wrote:
SELECT T1.id, T1.txt
FROM Table1 T1 JOIN Table2 T2 ON T1.Table2Id = T2.id
ORDER BY T2.order


It complains about the from-clause being wrong.
If I add "LEFT JOIN" instead of "JOIN", it runs like it should.
(thx btw :)

Can you explain why it didnt run with just "join"?


Since what John suggested is legal syntax in SQL Server (save that
he should have put the last "order" in brackets), I suspect that
you are not using SQL Server, but some other DB engine.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5
Erland Sommarskog wrote:
Since what John suggested is legal syntax in SQL Server (save that
he should have put the last "order" in brackets), I suspect that
you are not using SQL Server, but some other DB engine.


True, I only tested on Access. - The SQL server cant be reached from home :)

--
/Pip
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 01-09-2003
Jul 20 '05 #6
John Bell wrote:
Should work OK.


Yep. It worked on the SQL server at work...I only tested it on access
yesterday.

Thx :)

--
Pip
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 01-09-2003
Jul 20 '05 #7
PipHans (pi*****@hotmail.co) writes:
True, I only tested on Access. - The SQL server cant be reached from home


While both Access and SQL Server both claim to run SQL, there are
significant differences between the two - as there is about between
any pair of DBMSs.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #8

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

Similar topics

0
by: Mark Hahn | last post by:
Ville Vainio came up an idea for one final tweak of the tab/space indent algorithm we are using in Prothon, and the final result surprised me, in that it directly addresses the problem of mixed...
2
by: CLEAR-RCIC | last post by:
Hello All, I wrote a .dll that programatically maps two network drives and copies files from one drive to the other. The .dll works fine when using an .exe to call the .dll. When I call the...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
4
by: Joe Kimbler | last post by:
I've heard this both ways so I'm wondering, is using the: Me.Dispose () ....method in VB.NET the best way to close down an application? Is there a cleaner way to do it? I've heard grumblings...
50
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
4
by: metaperl | last post by:
I work at a place which is currently running SQL 2000, but they are planning to migrate to 2k5. I was thinking that this is the perfect opportunity to fix all the weaknesses we have had in our data...
4
by: chaitu | last post by:
Hi guys, I've written a parallel build program (in Perl) that takes a pre-computed dependency tree of many projects in 2 visual studio .net 2003 solution (.sln) files here at my company, and...
9
by: king kikapu | last post by:
Hi to all, i have started a month ago to seriously studying Python. I am now looking at the databases stuff and i want the opinion of more experienced Python programmers (than me) at the...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.