Databases :: MySql :: SQL Statement |
|||
| By: Angora |
Date: 14/10/2004 00:00:00 |
Points: 50 | Status: Answered Quality : Excellent |
|
i have 3 table, Table A, B and C Table A had (union, branch, refno ) Table B had (union, branch) Table C had (refno, manner) i would like to retrieve refno where A.union=B.union and A.branch = B.branch. At the same time i also like to retrive the count of (C.manner where manner='Y') where C.refno=A.refno Wat will be the SQL statement for these? |
|||
| By: VGR | Date: 14/10/2004 18:46:00 | Type : Answer |
|
| first you CAN'T create a table with a field named "union", it's a reserved word. I tested with zunion in stead and used At, Bt and Ct i.s.o. A,B,C : tets data : mysql> select * from At; +--------+--------+-------+ | zunion | branch | refno | +--------+--------+-------+ | 1 | 1 | 1 | | 2 | 1 | 1 | | 1 | 1 | 3 | +--------+--------+-------+ mysql> select * from Bt; +--------+--------+ | zunion | branch | +--------+--------+ | 1 | 1 | | 1 | 2 | | 1 | 3 | | 2 | 3 | +--------+--------+ mysql> select * from Ct; +-------+--------+ | refno | manner | +-------+--------+ | 1 | Y | | 2 | N | | 3 | Y | +-------+--------+ here do : drop table if exists tempo1; mysql> create temporary table tempo1 select refno,count(manner) as d from Ct where manner='Y' group by refno; (checking) mysql> select * from tempo1; +-------+---------------+ | refno | d | +-------+- + | 1 | 1 | | 3 | 1 | +-------+---------------+ 2 rows in set (0.01 sec) (performing your final query) mysql> select A.refno,C.d from At as A, Bt as B, tempo1 as C where A.zunion=B.zunion and A.branch = B.branch and C.refno=A.refno; +-------+---+ | refno | d | +-------+---+ | 1 | 1 | | 3 | 1 | +-------+---+ this is what you asked for. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








