Solved

INNER JOIN SQL query problems

Posted on 2010-09-10
6
381 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
[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
  • 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
Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

 
LVL 41

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 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

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…

617 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