Solved

MySQL - join 2 tables

Posted on 2011-03-04
3
526 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

626 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