Solved

MySQL - join 2 tables

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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