Unix Technical Forum

Plz, what is the most "correct" method

This is a discussion on Plz, what is the most "correct" method within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, Given 2 tables: Table1: id Auto,int,PrimKey table2id int txt nvarchar50 Table2: id Auto,int,PrimKey order int The scenario: Table2.order ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:50 PM
PipHans
 
Posts: n/a
Default 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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:51 PM
John Bell
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

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" <piphans@hotmail.co> wrote in message
news:3f5f47c2$0$48896$edfadb0f@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
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:51 PM
PipHans
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:51 PM
John Bell
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

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" <piphans@hotmail.co> wrote in message
news:3f5f7147$0$48899$edfadb0f@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
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 06:51 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

PipHans (piphans@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, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 06:52 PM
PipHans
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 06:52 PM
PipHans
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 06:52 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Plz, what is the most "correct" method

PipHans (piphans@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, sommar@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:27 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com