MySQL Forums
Forum List  »  Newbie

Re: Can't figure out how to build this query
Posted by: Barry Galbraith
Date: December 11, 2014 04:58PM

Is this what you mean?
/*Table structure for table `cases` */

DROP TABLE IF EXISTS `cases`;

CREATE TABLE `cases` (
  `id` int(11) NOT NULL,
  `caseid` varchar(20) DEFAULT NULL,
  `venueid` int(11) DEFAULT NULL,
  `disposition` int(11) DEFAULT NULL,
  `awarded` decimal(10,0) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `cases` */

insert  into `cases`(`id`,`caseid`,`venueid`,`disposition`,`awarded`)
 values (1,'24-0192',2,1,10000)
 ,(2,'24-1982',4,4,0)
 ,(3,'24-2983',2,1,29872);

/*Table structure for table `participants` */

DROP TABLE IF EXISTS `participants`;

CREATE TABLE `participants` (
  `pid` int(11) NOT NULL,
  `id` int(11) DEFAULT NULL,
  `ptype` int(11) DEFAULT NULL,
  `participant` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

/*Data for the table `participants` */

insert  into `participants`(`pid`,`id`,`ptype`,`participant`)
 values (1,1,1,'John Doe')
 ,(2,1,2,'Jane Doe')
 ,(3,2,1,'John Q. Public')
 ,(4,2,2,'United States of America')
 ,(5,3,1,'John Doe')
 ,(6,3,2,'United States of America');

 
mysql> SELECT
    -> c.id
    -> ,caseid
    -> ,p1.ptype
    -> ,p1.participant
    -> ,p2.ptype
    -> ,p2.participant
    ->  FROM cases c
    ->  JOIN participants p1
    ->  ON c.id = p1.id
    ->  JOIN participants p2
    ->  ON c.id = p2.id
    ->  WHERE  ((p1.ptype = 1 AND p1.participant LIKE '%John Doe%') AND (p2.ptyp
e = 2 AND p2.participant LIKE '%Jane Doe%'))
    ->  AND venueid = 2;
+----+---------+-------+-------------+-------+-------------+
| id | caseid  | ptype | participant | ptype | participant |
+----+---------+-------+-------------+-------+-------------+
|  1 | 24-0192 |     1 | John Doe    |     2 | Jane Doe    |
+----+---------+-------+-------------+-------+-------------+
1 row in set (0.00 sec)

Good luck,
Barry.

Options: ReplyQuote


Subject
Written By
Posted
Re: Can't figure out how to build this query
December 11, 2014 04:58PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.