Jump to content

Recommended Posts

    * your MySQL server version

 

5.0.75

 

    * the raw MySQL statement in question [in a CODE block, and without any PHP variables]

 

I'm trying to join these two queries:

 

"SELECT * FROM news WHERE news_id = $_GET[news_id]" (which will give me the user_id of the person who posted the news...)

 

to

 

"SELECT first_name, email FROM users WHERE user_id = $user_id" ($user_id being the value returned by the first query.)

 

What I tried was this:

 

"SELECT news.*, users.first_name, users.email FROM news, users WHERE news.news_id = ($_GET[news_id])"

 

Of course, at the time it didn't dawn on me that this query would return a random first_name and email because I don't specify anywhere that I want the first_name and email from the user who posted this particular news item.

 

 

 

    * any errors that MySQL returns to the client [from mysql_error()]

 

None - script runs fine, just returns incorrect data.

 

    * the table structure & column indexes of the relevant tables [via SHOW CREATE TABLE is preferred]

 

Users Table:

 

CREATE TABLE `users` (

`user_id` int(10) unsigned NOT NULL auto_increment,

`first_name` varchar(30) NOT NULL,

`last_name` varchar(30) NOT NULL,

`dob` varchar(10) NOT NULL,

`email` varchar(40) NOT NULL,

`user_name` varchar(30) NOT NULL,

`password` varchar(41) NOT NULL,

`tandc` char(1) NOT NULL,

`r_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

`confirmed` varchar(10) NOT NULL,

`access` tinyint(1) unsigned NOT NULL default '1',

PRIMARY KEY  (`user_id`)

) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1

 

News Table

 

CREATE TABLE `news` (

`news_id` int(10) unsigned NOT NULL auto_increment,

`user_id` int(10) unsigned NOT NULL,

`newscat_id` int(10) unsigned NOT NULL,

`news_headline` varchar(80) NOT NULL,

`news_content` mediumtext NOT NULL,

`submitted` datetime NOT NULL,

`confirmed` char(1) NOT NULL,

PRIMARY KEY  (`news_id`)

) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1

 

 

 

    * a clear and concise description of what you want this statement to achieve

 

I'm new at the JOIN queries - have always just done multiple queries to get the data I need.  I would like to be able to join queries to get all of the data for an entry in the news table and the first name and last name of the user who posted that entry.  I'm sure it's something easy and I just haven't seen it in a tutorial yet so can't wrap my head around the logic.

 

    * a description of what it's currently doing that's not to your liking

 

It currently returns information on the last user in the database.

 

    * a brief listing of the types of things you've attempted so far

 

I tried this:

 

"SELECT news.*, users.first_name, users.email FROM news, users WHERE news.news_id = ($_GET[news_id]) AND news.user_id = users.user_id"

 

But again, I'm not clarifying that I need the user associated with this particular news_id.

 

Thanks for all of your help!!

 

Tonya

 

Link to comment
https://forums.phpfreaks.com/topic/154607-solved-how-would-i-join-these/
Share on other sites

Wow, thanks so much :)

 

Can anyone recommend a tutorial somewhere that very clearly explains the JOIN queries?  I've read two now and don't seem to be getting it.  Neither included INNER JOIN as opposed to just JOIN, so I'm assuming that's something more advanced than the tutorials I've read.

 

Thanks again!

 

Tonya

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.