Solved

MySQL - join 2 tables

Posted on 2011-03-04
3
525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
Duboux earned 500 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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