visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Databases :: MySql :: SQL Statement


By: Angora U.S.A.  Date: 14/10/2004 00:00:00  English  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 English  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

EContact
browser fav
page generated in 297.494890 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page