Solved

INNER JOIN SQL query problems

Posted on 2010-09-10
6
376 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now