views:

59

answers:

4

I have two tables which I need to select all rows from where the userid is $userid then sort them. I tried to use join but I couldn't even really get started on how to get it right. Can anybody point me in the right direction as to how to make these into 1 query?

$result1 = mysql_query("SELECT * FROM paypal_sub_info 
             WHERE userid='$THEuserid' ORDER BY cur_time DESC");
$result2 = mysql_query("SELECT * FROM paypal_pay_info 
             WHERE userid='$THEuserid' ORDER BY cur_time DESC");

while($row = mysql_fetch_array($result1)){
    echo $row['txn_type'];
}

Solution:

SELECT * 
FROM   paypal_sub_info sub,paypal_pay_info pay 
WHERE  pay.userid = '$THEuserid' 
       AND sub.userid = '$THEuserid' 
ORDER  BY pay.cur_time DESC,sub.cur_time DESC 

Thanks for the help!

+3  A: 

Try this:

SELECT * FROM paypal_sub_info sub, paypal_pay_info pay 
WHERE pay.userid='$THEuserid' AND sub.userid='$THEuserid'
ORDER BY pay.cur_time DESC, sub.cur_time DESC

If you just want 'txn_type', you could make it a SELECT pay.txn_type AS txn_type

Borealid
Say's it wasn't valid. There a several field with common names, cur_time is one...
Derek
OMG Ponies
I used this solution except changed the ORDER BY to "ORDER BY pay.cur_time DESC, sub.cur_time DESC" Thanks!!!
Derek
@Derek: Sorry about that, edited, glad you could figure it out.
Borealid
+1  A: 

I believe you want:

    SELECT field1, field2, ... FROM paypal_sub_info WHERE userid='$THEuserid'
    UNION
    SELECT field1, field2, ... FROM paypal_pay_info WHERE userid='$THEuserid'
    ORDER BY cur_time DESC
Will A
That assumes the columns, at least their data types, fall in identical order. I really don't think it's what the OP is after, myself
OMG Ponies
Yeah, different number of columns, gave me that error.
Derek
Good point - I'll engage brain before keyboard next time. I think the op might be after a UNION here, tho' - given the ORDER BYs (which could be erroneous).
Will A
+1  A: 

Use:

SELECT psi,*, ppi.*
   FROM PAYPAL_SUB_INFO psi
   JOIN PAYPAL_PAY_INFO ppi ON ppi.userid = psi.userid
  WHERE psi.userid = $THEuserid
ORDER BY psi.cur_time DESC, ppi.cur_time DESC
OMG Ponies
A: 

So first off consider using mysqli for for any serious project. OMG Ponies answer is how I would suggest doing it, thought you shouldn't have to specify the alias.wildcard fields separately in the select clause. It's also a best practice to actually specify the fields you are trying to fetch rather than *, though we all use * a lot when we're lazy.

Will A's answer makes me smile because it's technically what you asked for, though not what I expect you wanted.

Do you have a more detailed description of what data you're trying to extract, or was this just an example because you are having trouble figuring out joins?

-J

umassthrower
You have to specify the alias because there's a `cur_time` column in both tables - without the alias, you'll get an "ambiguous column reference" error.
OMG Ponies
which I did infact get...
Derek
I agree there is an ambiguous column but I just ran tests on mysql 5.0.51/php 5.2.4: print_r(mysql_fetch_assoc(mysql_query('select * from portfolios p join portfolio_images pi on p.id = pi.portfolio_id'))); with schemas of: portfolio - id, title, customer_name, type, descriptionportfolio_images - id, title, portfolio_id, path, descriptionand got identical results for both * and p.*, pi.*Array( [id] => 1 [title] => Wide [customer_name] => [type] => [description] => [portfolio_id] => 1 [path] => static/images/from_pp/curvey_bush_1.jpg)please edificate me :-)
umassthrower
Still identical results when I add an order by clause.
umassthrower
Perhaps I'm beating a dead horse, but given Derek's chosen solution and my test I'm pretty certain that my assertion that you "shouldn't have to specify the alias.wildcard field separately in the select clause" is correct. :-)-J
umassthrower
naw, I just said in my case that when I tested it I got that same "ambiguous column reference" error.
Derek
The ambiguous column reference was because of the order by clause not the select clause. I was making reference to the fact that the chosen answer has `select *` in it not `select alias1.*, alias2.*`.
umassthrower