Solved

INNER JOIN SQL query problems

Posted on 2010-09-10
6
379 Views
Last Modified: 2012-06-27
Unable to get this to work:

SELECT `listingsdb_id` FROM `ejunkie_paidlistings`, `default_en_listingsdb` INNER JOIN `default_en_listingsdb` ON ejunkie_paidlistings.listingsdb_id = default_en_listingsdb.listingsdb_id WHERE `listingsdb_expiration` != '2011-09-15'

Getting al kinds of errors including the last: #1052 - Column 'listingsdb_id' in field list is ambiguous

The databases are:

 
--
-- Table structure for table `ejunkie_paidlistings`
--

CREATE TABLE IF NOT EXISTS `ejunkie_paidlistings` (
  `id` int(10) NOT NULL auto_increment,
  `listingsdb_id` int(10) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6433 ;

--
-- Dumping data for table `ejunkie_paidlistings`
--

INSERT INTO `ejunkie_paidlistings` (`id`, `listingsdb_id`) VALUES
(1, 467);

Open in new window


and
--
-- Table structure for table `default_en_listingsdb`
--

CREATE TABLE IF NOT EXISTS `default_en_listingsdb` (
  `listingsdb_id` int(11) NOT NULL auto_increment,
  `userdb_id` int(11) NOT NULL default '0',
  `listingsdb_title` varchar(80) NOT NULL default '',
  `listingsdb_expiration` date NOT NULL default '0000-00-00',
  `listingsdb_notes` text NOT NULL,
  `listingsdb_creation_date` date NOT NULL default '0000-00-00',
  `listingsdb_last_modified` datetime NOT NULL default '0000-00-00 00:00:00',
  `listingsdb_hit_count` int(11) NOT NULL default '0',
  `listingsdb_featured` char(3) NOT NULL default '',
  `listingsdb_active` char(3) NOT NULL default '',
  `listingsdb_mlsexport` char(3) NOT NULL default '',
  `listingsdb_notified` char(3) NOT NULL default 'no',
  `flagged` char(3) NOT NULL default '',
  `flagdate` date NOT NULL default '0000-00-00',
  PRIMARY KEY  (`listingsdb_id`),
  KEY `idx_active` (`listingsdb_active`),
  KEY `idx_user` (`userdb_id`),
  KEY `idx_mlsexport` (`listingsdb_mlsexport`),
  KEY `idx_listfieldmashup` (`listingsdb_id`,`listingsdb_active`,`userdb_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9132 ;

--
-- Dumping data for table `default_en_listingsdb`
--

INSERT INTO `default_en_listingsdb` (`listingsdb_id`, `userdb_id`, `listingsdb_title`, `listingsdb_expiration`, `listingsdb_notes`, `listingsdb_creation_date`, `listingsdb_last_modified`, `listingsdb_hit_count`, `listingsdb_featured`, `listingsdb_active`, `listingsdb_mlsexport`, `listingsdb_notified`, `flagged`, `flagdate`) VALUES
(223, 60, 'This is the 1 acre Lot', '2009-07-30', '', '2008-01-06', '2008-01-06 01:24:11', 511, 'no', 'no', 'no', 'yes', '', '0000-00-00');

Open in new window

0
Comment
Question by:greenerpastures
  • 2
  • 2
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33650245
you have to allias your column because it doesn't know which table you want listingsdb_id from.  Also you only need quotes around the date:

SELECT ejunkie_paidlistings.listingsdb_id FROM ejunkie_paidlistings, default_en_listingsdb INNER JOIN default_en_listingsdb ON ejunkie_paidlistings.listingsdb_id = default_en_listingsdb.listingsdb_id WHERE listingsdb_expiration != '2011-09-15'
0
 

Author Comment

by:greenerpastures
ID: 33650274
OK, I tried your statement and got this error:
#1066 - Not unique table/alias: 'default_en_listingsdb'
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33650325
I think you have listngsdb twice on the right side of =
ON ejunkie_paidlistings.listingsdb_id = default_en_listingsdb.listingsdb_id

maybe you need:

ON ejunkie_paidlistings.listingsdb_id = default_en.listingsdb
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 33650989
try this.
SELECT e.listingsdb_id 
  FROM ejunkie_paidlistings e 
       INNER JOIN default_en_listingsdb d 
         ON e.listingsdb_id = d.listingsdb_id 
 WHERE d.listingsdb_expiration != '2011-09-15'

Open in new window

0
 

Author Comment

by:greenerpastures
ID: 33651178
Sharath,

IT works, but one question:
Why do I get two columns in the results, including id?

I just need to SELECT one column listingsdb_id from ejunkie_paidlistings


RESULT:
INSERT INTO `ejunkie_paidlistings` (`id`, `listingsdb_id`) VALUES
(1, 467),
(2, 982),
0
 
LVL 40

Expert Comment

by:Sharath
ID: 33651216
>> Why do I get two columns in the results, including id?

You won't get two columns in the result as there is only one column in the SELECT clause. Did you run the query and see the result?
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 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