Simplify a long sql statement

This is a discussion on Simplify a long sql statement within the MySQL Database forums, part of the Database Forums category; Hi there, this statement works fine but I'm wondering is there any way to simplify it? SELECT score, (SELECT ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-12-2008
Ciaran
 
Posts: n/a
Default Simplify a long sql statement

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 (permalink)  
Old 05-12-2008
strawberry
 
Posts: n/a
Default Re: Simplify a long sql statement

On 12 May, 13:54, Ciaran <cronok...@hotmail.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!
Reply With Quote
  #3 (permalink)  
Old 05-12-2008
Ciaran
 
Posts: n/a
Default Re: Simplify a long sql statement

On May 12, 3:35 pm, strawberry <zac.ca...@gmail.com> wrote:
> On 12 May, 13:54, Ciaran <cronok...@hotmail.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 (permalink)  
Old 05-12-2008
Rik Wasmus
 
Posts: n/a
Default Re: Simplify a long sql statement

On Mon, 12 May 2008 18:06:57 +0200, Ciaran <cronoklee@hotmail.com> wrote:

> On May 12, 3:35 pm, strawberry <zac.ca...@gmail.com> wrote:
>> On 12 May, 13:54, Ciaran <cronok...@hotmail.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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:19 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0