Jump to content

Taking data from two tables


KingOfHeart

Recommended Posts

I have a site that contains a file list . I also have a wiki that contains a file list. I want to list both of these files into one group.

 

From Wiki

image - Table

img_name - Contains the name of the file

img_description - Contains the description

img_user - Returns the number of the user who uploaded it

 

user - Table

user_id - The id for the user

user_name - Contains the name of the user

 

From my File List

Downloads - Table

name - Name of the file

userid - The user's id

description - The description for the file

type - The file's Type

 

Accounts - Table

id - The user's ID

username - The user's name

 

So first we need to get it organized for the image table to be combined with the user table. Then Downloads needs to be combined with Accounts. After all that is done, the two lists need to come together.

 

So how tough would this be? I'd settle for keeping the Wiki data and the File List separated into two lists if necessary.

 

Link to comment
Share on other sites

Try this:

( 
select
i.img_name
, i.img_description
, u.user_name
from image as i
inner join user at t on ( t.user_id = i.img_user )
)
union all
( 
select d.name
, d.description
, a.username
from downloads as d
inner join accounts as a on ( a.id = d.userid )
)

Link to comment
Share on other sites

How do I set this up?

 

$sql = mysql_query("(
select
i.img_name
, i.img_description
, u.user_name
from image as i
inner join user at t on ( t.user_id = i.img_user )
)
union all
(
select d.name
, d.description
, a.username
from downloads as d
inner join accounts as a on ( a.id = d.userid )
)
");

while($rows= mysql_fetch_array($sql))
{
$out .= $rows['name'];
}

 

$out is for just returning the data.

 

Link to comment
Share on other sites

fixed some errors in it, and got it decent

 

$sql = mysql_query("(
select
i.img_name
, i.img_description
, t.user_name
from image as i
inner join user as t on ( t.user_id = i.img_user )
)
union all
(
select d.name
, d.description
, a.username
from Downloads as d
inner join Accounts as a on ( a.id = d.userid )
)
");

 

mind you, due to the fact that the fields of the first group and second group don't match, you can't use a union here, the only way would be if they each had the same fields.

Link to comment
Share on other sites

mind you, due to the fact that the fields of the first group and second group don't match, you can't use a union here, the only way would be if they each had the same fields.

Not true at all... like I said earlier, use column aliases to make them the same.

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.