Jump to content

2 Table Search Query


brussell

Recommended Posts

Trying to SELECT * from 2 tables (customerdetails & rackallocation (as below)) but am getting ambiguous errors for the 'companyname' column due to it being a primary key in 'customerdetails' and foreign key in 'rackallocation'. Please can you help spot my error or advise on additional code required.

CREATE TABLE `customerdetails` (
`companyname` varchar(50) NOT NULL, 
`contactname` varchar(30) default NULL,
`phonenumber` varchar(15) default NULL,
`emailaddress` varchar(50) default NULL,
PRIMARY KEY  (`companyname`))
ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE `rackallocation` (
`racklocation` varchar(15) NOT NULL,
`racktype` char(4) default NULL,
`companyname` varchar(50) NOT NULL,
`gwname` varchar(30)default NULL, 
PRIMARY KEY  (`racklocation`),
KEY `companyname` (`companyname`),
FOREIGN KEY (`companyname`) REFERENCES `customerdetails` (`companyname`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=latin1

QUERY:
$query = "select * from customerdetails, rackallocation where ".$searchtype." like '%".$searchterm."%' and customerdetails.companyname = rackallocation.companyname";
$result = mysql_query($query) or die('Problem with query: ' . $query . '<br />' . mysql_error());
$num_results = mysql_num_rows($result);

Link to comment
Share on other sites

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.