HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Problem joining two tables in MySQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
problemtablestwomysqljoining

Problem

I'm new to MySQL. I'm having trouble in joining two tables:

SELECT e.resourceName
 FROM resources_in_room r
 JOIN resource e ON r.resourceId=e.resourceId WHERE r.roomId=2;


Table resources in room:

CREATE TABLE  ``resources_in_room` (
  `id` bigint(20) NOT NULL auto_increment,
  `Quantity` int(11) default NULL,
  `resourseId` int(11) default NULL,
  `roomId` int(11) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `resourseId` (`resourseId`,`roomId`),
  KEY `FK36360B9B821B4697` (`roomId`),
  KEY `FK36360B9BC0C935AD` (`resourseId`),
  CONSTRAINT `FK36360B9BC0C935AD` 
    FOREIGN KEY (`resourseId`) REFERENCES `resource` (`ResourceId`),
  CONSTRAINT `FK36360B9B821B4697`
    FOREIGN KEY (`roomId`) REFERENCES `room` (`RoomId`)
)


Table resource:

CREATE TABLE  `resource` (
  `ResourceId` int(11) NOT NULL auto_increment,
  `Description` varchar(255) default NULL,
  `Quantity` int(11) default NULL,
  `ResourceName` varchar(255) default NULL,
  PRIMARY KEY  (`ResourceId`)
)

Solution

its resourseId in your table definition and you are using resourceId in query.

This will work

SELECT e.resourceName FROM resources_in_room r join resource e on r.resourseId = e.resourceId where r.roomId=2;

Code Snippets

SELECT e.resourceName FROM resources_in_room r join resource e on r.resourseId = e.resourceId where r.roomId=2;

Context

StackExchange Database Administrators Q#22265, answer score: 4

Revisions (0)

No revisions yet.