Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2013, 10:25 AM   PM User | #1
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
Selecting from two tables

Okay so I pretty much know nothing of this, but what I'm trying to do is this:

I want to select all columns from a table called "updates", and one column (named "image") from a table called "portfolio".

I've been trying some things but I don't really have a clue and all I'm getting are errors.

When I look up on how to select information from to tables, I find all things like joining them with inner join and left join etc. But I was wondering what I need to do in this case and if these things are necessary.
apotd is offline   Reply With Quote
Old 01-12-2013, 05:59 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,382
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
I think you need to have something common with both tables in order to do this.

Like "ON TableA.name = TableB.name". With out it I keep coming up with way too much information.
sunfighter is offline   Reply With Quote
Old 01-12-2013, 07:13 PM   PM User | #3
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
Mh but they have nothing in common, that's not my meaning because I just have this page, on which I want to display updates (just a date and the title of the update, which I have a table for). And on the same page I want to make a div in which random images from another table are displayed.

Do I maybe just have to make a new query for that or something? Because I can't really see how to link those two.
apotd is offline   Reply With Quote
Old 01-12-2013, 08:49 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, I know one way to do it, but it would *NOT* guarantee that each record from your primary table would get a *UNIQUE* image from the second table.

In other words, if you showed 10 dates and titles, you might find that you would get 7 different images, with 3 of the images repeated. etc.

And I do see another more complex way to do this where each date/title would get a unique image *provided* there are at least as many image records as there are data/title records.

But, really, it would probably be easier to just do this as two separate queries and let your PHP/ASP/JSP/whatever code get one record from each table.

You could do both
Code:
SELECT * FROM dates_and_titles ORDER BY something;
SELECT imageURL FROM images ORDER BY RAND();
And just run a loop where you get one record from each query each time through the loop.

But if you really need a MySQL solution we could make one.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-12-2013, 10:43 PM   PM User | #5
apotd
New Coder

 
Join Date: Oct 2011
Posts: 75
Thanks: 1
Thanked 0 Times in 0 Posts
apotd is an unknown quantity at this point
The easy way that doesn't guarantee unique images is fine. But I'm a bit confused. To get my records from the updates table I use this

Code:
$query = "SELECT *FROM updates ORDER BY date DESC";
So for that
Code:
SELECT images FROM portfolio ORDER BY RAND();
part, where do I put it? In the same query? Or do I have to make a new query? I'm sorry I'm really completely new to this and when I try putting it together I'm getting errors.
apotd is offline   Reply With Quote
Old 01-13-2013, 08:16 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,188
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Two separate queries.

I don't use PHP, but it will be *SOMETHING* like this:
Code:
$r1 = mysql_query("SELECT * FROM updates ORDER BY date DESC") or die(mysql_error());
$r2 = mysql_query("SELECT images FROM portfolio ORDER BY RAND()") or die(mysql_error());
while ( $rows1 = mysql_fetch_array($r1) )
{
    $rows2 = mysql_fetch_array($r2) or die(mysql_error());
    $image = $rows2["images"];
    ... now output info for one record from $rows1 ...
    ... including that image from $rows2 ...
}
CAUTION: If you don't have at least as many images as you have records in UPDATES table, you will run out of records from $rows2 before you get to the end of $row1.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.