Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL - join 2 tables

Posted on 2011-03-04
3
Medium Priority
?
535 Views
Last Modified: 2012-05-11
Hi,
I have 2 tables that I join with MySQL query below:

$query_mem_directory= "SELECT *
FROM `members_directory` AS members
JOIN category AS cat
WHERE cat.id = members.category
AND user_id = '9'"

$rs_directory = mysql_query($query_mem_directory, $conn_data) or die('error retrieving');
$row_rs_directory = mysql_fetch_assoc($rs_directory);

The results set produces 2 columns with name of "id"
How can I retrieve the value for the first id as $row_rs_directory['id'] will display value of the second id column?

id  user_id  updated  date_listed  date_expires  category  business_address  postal_address  busname  contact  phone  fax  mobile  email  website  blurb  more_details  image1  id  position  cat  subcat  min_order  cat_type  active  

Thanks in advance for your comments.


CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL auto_increment,
  `position` int(5) default '10',
  `cat` varchar(40) default 'Main',
  `subcat` varchar(50) NOT NULL,
  `min_order` tinyint(3) default NULL,
  `cat_type` varchar(4) default 'GN',
  `active` char(3) default 'Y',
  UNIQUE KEY `id_2` (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=141 ;




CREATE TABLE IF NOT EXISTS `members_directory` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(6) default NULL,
  `updated` varchar(3) default 'N',
  `date_listed` varchar(15) default NULL,
  `date_expires` varchar(15) default NULL,
  `category` varchar(255) default NULL,
  `business_address` varchar(255) default NULL,
  `postal_address` varchar(255) default NULL,
  `busname` varchar(255) default NULL,
  `contact` varchar(255) NOT NULL,
  `phone` varchar(255) default NULL,
  `fax` varchar(255) default NULL,
  `mobile` varchar(255) default NULL,
  `email` varchar(255) default NULL,
  `website` varchar(255) default NULL,
  `blurb` text,
  `more_details` text,
  `image1` varchar(60) default 'uploads/nopic.jpg',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=819 ;
0
Comment
Question by:sabecs
3 Comments
 
LVL 3

Accepted Solution

by:
Duboux earned 2000 total points
ID: 35041535
Few things:

1)
Sometimes MySql rewrites the 2nd column with the same name to columname1: id1


2)
You could do the rewriting part yourself:
SELECT
members.id AS id_mem,
cat.id AS id_cat,
members.*,
cat.*
FROM `members_directory` AS members
JOIN category AS cat
WHERE cat.id = members.category
AND user_id = '9'


3)
To prevent these problems, don't use the same column names within the same database ;)
Use for instant mem_id instead of id ;)
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 35041536
It is recommended to specify the field names you will need instead of using "*" even if you will be needing all the fields. In case you have identical field names, you can assign aliases and use that to reference the field.

e.g. You can use CatID to reference the id field from category table in this query
$query_mem_directory= "SELECT members.id, members.position, cat.id `CatID`, cat.user_id
FROM `members_directory` AS members
JOIN category AS cat
WHERE cat.id = members.category
AND user_id = '9'"

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 35041612
Thanks..
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question