dbTalk Databases Forums  

Simplify a long sql statement

comp.databases.mysql comp.databases.mysql


Discuss Simplify a long sql statement in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ciaran
 
Posts: n/a

Default Simplify a long sql statement - 05-12-2008 , 07:54 AM






Hi there,
this statement works fine but I'm wondering is there any way to
simplify it?

SELECT score,
(SELECT name from players WHERE playerID=records.playerID LIMIT 1) as
playername,
(SELECT name from teams WHERE teamID=records.teamID LIMIT 1) as
teamname
FROM records

Thanks for any help!
Ciarán

Reply With Quote
  #2  
Old   
strawberry
 
Posts: n/a

Default Re: Simplify a long sql statement - 05-12-2008 , 09:35 AM






On 12 May, 13:54, Ciaran <cronok... (AT) hotmail (DOT) com> wrote:
Quote:
Hi there,
this statement works fine but I'm wondering is there any way to
simplify it?

SELECT score,
(SELECT name from players WHERE playerID=records.playerID LIMIT 1) as
playername,
(SELECT name from teams WHERE teamID=records.teamID LIMIT 1) as
teamname
FROM records

Thanks for any help!
Ciarán

SELECT r.score, p.name, t.name
FROM records r
LEFT JOIN players p
ON p.playerid = r.playerid
LEFT JOIN teams
ON t.team_id = r.team_id;


I'm not sure what all that "LIMIT 1" stuff is supposed to do in this
context!


Reply With Quote
  #3  
Old   
Ciaran
 
Posts: n/a

Default Re: Simplify a long sql statement - 05-12-2008 , 11:06 AM



On May 12, 3:35 pm, strawberry <zac.ca... (AT) gmail (DOT) com> wrote:
Quote:
On 12 May, 13:54, Ciaran <cronok... (AT) hotmail (DOT) com> wrote:

Hi there,
this statement works fine but I'm wondering is there any way to
simplify it?

SELECT score,
(SELECT name from players WHERE playerID=records.playerID LIMIT 1) as
playername,
(SELECT name from teams WHERE teamID=records.teamID LIMIT 1) as
teamname
FROM records

Thanks for any help!
Ciarán

SELECT r.score, p.name, t.name
FROM records r
LEFT JOIN players p
ON p.playerid = r.playerid
LEFT JOIN teams
ON t.team_id = r.team_id;

I'm not sure what all that "LIMIT 1" stuff is supposed to do in this context!

Thanks Strawberry, I'll have to read up on left joins! I assumed
adding LIMIT 1 would stop the query from searching after it had found
1 match, making it a little quicker, but I suspect I might be
mistaken?



Reply With Quote
  #4  
Old   
Rik Wasmus
 
Posts: n/a

Default Re: Simplify a long sql statement - 05-12-2008 , 01:02 PM



On Mon, 12 May 2008 18:06:57 +0200, Ciaran <cronoklee (AT) hotmail (DOT) com> wrote:

Quote:
On May 12, 3:35 pm, strawberry <zac.ca... (AT) gmail (DOT) com> wrote:
On 12 May, 13:54, Ciaran <cronok... (AT) hotmail (DOT) com> wrote:

Hi there,
this statement works fine but I'm wondering is there any way to
simplify it?

SELECT score,
(SELECT name from players WHERE playerID=records.playerID LIMIT 1) as
playername,
(SELECT name from teams WHERE teamID=records.teamID LIMIT 1) as
teamname
FROM records

Thanks for any help!
Ciarán

SELECT r.score, p.name, t.name
FROM records r
LEFT JOIN players p
ON p.playerid = r.playerid
LEFT JOIN teams
ON t.team_id = r.team_id;

I'm not sure what all that "LIMIT 1" stuff is supposed to do in this
context!


Thanks Strawberry, I'll have to read up on left joins! I assumed
adding LIMIT 1 would stop the query from searching after it had found
1 match, making it a little quicker, but I suspect I might be
mistaken?
Probably, the playerid is a PRIMARY KEY or at least unique, so the engine
is smart enough to just return the only player which can have that id.
--
Rik Wasmus
[SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work to
fund a self developed bigger project, mail me at rik at rwasmus.nl. [/SPAM]


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.