|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
SQL Select Distinc() from 2 tables?
2 Tables, result_p & result_nw, stores 2 different kinds of results... however... both tables contains cid that represent the id for a team... Both tables can contain the same Id (cid) more than one time.
What I like to do is create a list of all cid's from both tables and only select them once... Code:
$sql = "SELECT t1.DISTINCT(cid), t1.cname, t2.DISTINCT(cid), t2.cname FROM result_p AS t1 result_nw AS t2 GROUP BY cid ORDER BY cname ASC"; This one obviously doesn't work... Any suggestions? |
|
#2
|
||||
|
||||
|
Would this work?
Code:
SELECT DISTINCT t1.cid, t1.cname, t2.cid, t2.cname FROM result_p AS t1, result_nw AS t2 GROUP BY t1.cid ORDER BY cname ASC Is there a relation between cid's? |
|
#3
|
|||
|
|||
|
A UNION may suit you:
Code:
(SELECT DISTINCT cid, cname FROM result_p) UNION ALL (SELECT DISTINCT cid, cname FROM result_nw) ORDER BY cname ASC
__________________
"A pawn is the most important piece on the chessboard -- to a pawn" |
|
#4
|
||||
|
||||
|
Note: to get every record only ONCE, you shouldn't do UNION ALL, but UNION DISTINCT
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 275
![]() |
|
#5
|
|||
|
|||
|
IRT: MadCow,
Relation between cids? Not really, they just apear in both tables. Sure, they represent the same "team" in both tables but I am currently not using the cid's to pick data from another table. Did I answer that question or is your question more advanced? ![]() IRT: MadPawn I'll give that a shot...will let you know about the outcome. |
|
#6
|
|||
|
|||
|
Quote:
Yes, this is the finished code, and it works perfect. Thanks guys. $sql = "(SELECT DISTINCT cid , cname FROM result_p) UNION DISTINCT (SELECT DISTINCT cid, cname FROM result_nw) Decided I wanted to count them aswell: $sql = "(SELECT DISTINCT COUNT(cid) FROM result_p) UNION DISTINCT (SELECT DISTINCT COUNT(cid) FROM result_nw)"; |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > SQL Select Distinc() from 2 tables? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|