I have problems to query my data. Here are my tables:
CREATE TABLE `A` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`myString` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `B` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`myStringId1` int(10) NOT NULL,
`myStringId2` int(10) NOT NULL,
`value` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index1` (`myStringId1`),
KEY `index2` (`myStringId2`),
CONSTRAINT `fk_B2` FOREIGN KEY (`myStringId2`) REFERENCES `A` (`id`),
CONSTRAINT `fk_B1` FOREIGN KEY (`myStringId1`) REFERENCES `A` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
where myStringId1 and myStringId2 have foreign key constraints to TABLE A (id)
Ok let me clarify what I want to achieve by query:
Lets assume some values for the given tables:
TABLE A:
ROW1: 22, "foo"
ROW2: 33, "bar"
TABLE B:
ROW1: 1, 22, 33, "true"
ROW2: 2, 22, 22, "false"
Now what I need is a query where I give String “foo” and get the following result from joining the two tables:
RESULT:
ROW1: "foo","bar", "true"
ROW2: "foo", "foo", "false"
given String “bar” I expect this:
RESULT:
ROW1: “foo”,”bar”,”true”
any ideas ?
After reading your comment I think this is closer to what you need:
The two joins will ensure the both
b.myStringId1andb.myStringId2are existing keys forTABLE A, and theWHEREensures that one of those 2 strings is the one you need.