If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > a field from the same table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-19-09, 04:30
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
a field from the same table

hi to all,
anyone knows how to make this work?
it gives me an error.
i want a field from the same table with where condition
Code:
SELECT
PawnTicketNo
,LastName
, DateLoan
, DateRedeem
( select DateLoan from pawnshop_tbl where servicetype = 'renew' )
FROM pawnshop_tbl
thanks in advance.
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #2 (permalink)  
Old 01-19-09, 04:37
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by homer.favenir
anyone knows how to make this work?
  • You seem to be missing a comma after DateRedeem.
  • I think you also want to be using the if() function rather than the sub query.
  • The subquery could return more than one row at any point which I'm guessing is not what your after.
Mike
Reply With Quote
  #3 (permalink)  
Old 01-19-09, 04:55
homer.favenir homer.favenir is offline
Registered User
 
Join Date: Oct 2007
Location: Manila, Philippines
Posts: 132
Quote:
The subquery could return more than one row at any point which I'm guessing is not what your after.
yes it is,
Quote:
I think you also want to be using the if() function rather than the sub query.
yes, youre right. instead of using subquery, i should use if() statement.

Code:
SELECT
PawnTicketNo
,LastName
, DateLoan
, DateRedeem
, if( ServiceType = 'Renew', DateLoan, '' ) as 'Date Renew'
FROM pawnshop_tbl p;
thanks a lot!
__________________
Take Nothing But Pictures;
Leave Nothing But Footprints;
Kill Nothing But Time;
Reply With Quote
  #4 (permalink)  
Old 01-19-09, 08:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
use a CASE expression, which is standard SQL, not IF, which is proprietary to mysql
Code:
CASE WHEN ServiceType = 'Renew' THEN DateLoan END as 'Date Renew'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On