Jump to content

[SOLVED] combine two db tables with queries.


seany123

Recommended Posts

i know this is like one of the first things i should have learnt but im still very fuzzy on this..

 

 

i want to basically join these two tables with these queries...

 

 

<?php
$query = $db->execute("select `id`, `username`, `prize_money`, `time`  from `lottery` order by `time` desc limit 100");
while($lotterywinner = $query->fetchrow())

$query = $db->execute("select `id`, `username`, `level`, `money`, `last_active`, `banned`, `staff`, `rm`, `ncolor`, `ncolorid`, `city_id` from `players`");
while($member = $query->fetchrow())
?>

 

i know the queries migght not be coded correctly.. but if someone could just give me an example of how its done then i can do the rest.

 

thanks

 

Seany123

Link to comment
Share on other sites

Hi

 

Are they 2 seperate tables? First query doesn't have a from clause.

 

All the best

 

Keith

 

yeah they are two different tables from the same database...

 

i pasted the wrong query... so its been edited now.

 

 

to give you an idea of what im trying to do....

 

 

the first query is a lottery tables... everyday 1 player wins the lottery and is added to that table....

 

 

(im having a html table to show all the winners....)

 

 

the second query is needed to get the winners other infomation

Link to comment
Share on other sites

Hi

 

Something like this:-

 

$query = $db->execute("select a.id, a.username, a.level, a.money, a.last_active, a.banned, a.staff`, a.rm, a.ncolor, a.ncolorid, a.city_id, b.id, b.username, b.prize_money, b.time

FROM `players` a

LEFT OUTER JOIN `lottery` b

ON a.username = b.username order by `time` desc");

 

That would bring you back one or more rows player with matching lottery info from the other table. However depending on your data it could bring the same user back multiple rows for one player, one for each matching record on the lottery table.

 

All the best

 

Keith

Link to comment
Share on other sites

Try...

 

$query = $db->execute("select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.id, l.username, l.prize_money, l.time from `players` p, `lottery` l where p.id=l.id");
while($member = $query->fetchrow())

Link to comment
Share on other sites

Try...

 

$query = $db->execute("select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.id, l.username, l.prize_money, l.time from `players` p, `lottery` l where p.id=l.id");
while($member = $query->fetchrow())

 

just tried that and got this error...

 

Fatal error: Call to a member function on a non-object on line 29

 

line 29:

 

<?php

while($member = $query->fetchrow())

?>

Link to comment
Share on other sites

This is what i got from phpmyadmin.

 

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

 

ERROR: Unknown Punctuation String @ 13

STR: -&

SQL: $query = $db->execute("select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.id, l.username, l.prize_money, l.time from `players` p, `lottery` l where p.id=l.id")

SQL query:

 

$query = $db->execute("select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.id, l.username, l.prize_money, l.time from `players` p, `lottery` l where p.id=l.id")

 

MySQL said:

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = $db->execute("select p.id, p.username, p.level, p.money

Link to comment
Share on other sites

Hi

 

Just the SQL in phpmyadmin. Rest is php code, not SQL

 

select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.id, l.username, l.prize_money, l.time from `players` p, `lottery` l where p.id=l.id

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Just the SQL in phpmyadmin. Rest is php code, not SQL

 

select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.id, l.username, l.prize_money, l.time from `players` p, `lottery` l where p.id=l.id

 

All the best

 

Keith

 

okay sorry about that i rarely use phpmyadmin to actually do any sql work.

 

here's the error:

 

#1054 - Unknown column 'l.id' in 'field list'

 

 

well basically that means it cant find id in my lottery table... reason being there isnt an id value in my lottery table.

 

 

so i tried changeing it to this...

 

select p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id,  l.username, l.prize_money, l.time from `players` p, `lottery` l where p.username=l.username

 

thinking it would join using the username...

 

and got this error:

 

MySQL said:

 

#1267 - Illegal mix of collations (latin1_general_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='

 

Link to comment
Share on other sites

oops i see.... i changed collations to the same and....

 

 

 

SQL query:

SELECT p.id, p.username, p.level, p.money, p.last_active, p.banned, p.staff, p.rm, p.ncolor, p.ncolorid, p.city_id, l.username, l.prize_money, l.time

FROM  `players` p,  `lottery` l

WHERE p.username = l.username

LIMIT 0 , 30

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.