Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

INNER JOIN SQL query problems

Posted on 2010-09-10
6
Medium Priority
?
385 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 41

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 41

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 41

Accepted Solution

by:
Sharath earned 2000 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 41

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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

971 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