|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
||||
|
||||
|
Using Joins - results aren't what I expected
See this post for background info & to see the other things I tried:
http://forums.devshed.com/showthread.php?p=691829 ---------------------------------------- I'm not sure how well I can explain this without writing a novel. I want to get all of the subcategories for a given category and list them. I have 2 tables for this, one is categories and one is subcategories: categories table: --------------------------- catid | title | descript | --------------------------- 1 | Forms | find forms | --------------------------- 2 | Legal | legal forms| --------------------------- 3 | Tax | tax forms | --------------------------- subcategories table: ----------------- catid | parent | ----------------- 2 | 1 | ----------------- 3 | 1 | ----------------- I want to now select all of the subcategories for the Forms category. But this snippet of code below returns the parent row instead of the subcategories. PHP Code:
The code above returns: 1 | Forms | find forms | When I want it to return: 2 | Legal | legal forms| --------------------------- 3 | Tax | tax forms |
__________________
Thanks Talia http://www.tagyoureit.org http://web2learning.net http://www.gamecrafters.net Last edited by talia679 : June 25th, 2004 at 01:36 PM. |
|
#2
|
||||
|
||||
|
What did you get when you tried this (reposting for convenience from PHP thread) --
Code:
SELECT c3.catid, c3.title, c3.shortDescrip, c3.meta, c3.seealso FROM categories c1 LEFT JOIN subcategories c2 ON c1.catid = c2.parent LEFT JOIN categories c3 ON c3.catid = c2.catid WHERE c1.catid = 1 I created some test tables and ran this, and it worked fine, though I was only going off of the name and descript columns. [Edit]I just noticed this is MS SQL -- I assumed you were using MySQL, and the above may not be transferable. So, erm, nevermind. ![]()
__________________
"Not to offend our Swedish listeners ... if we have any, that is—" "—But your team's rubbish." (Sun webcast, Sweden vs. Paraguay) Who needs corporate radio? WeFunkRadio.com | Global Pop Conspiracy | Radio Paradise | SomaFM | The Classic Soul Network | Boot Liquor | WFMU Freeform Radio Last edited by Ucht : June 25th, 2004 at 01:45 PM. |
|
#3
|
||||
|
||||
|
I posted in the wrong forum!!! OOPSS! I'll move my post to MySQL!!!
|
|
#4
|
||||
|
||||
|
Does this do the trick?
$query = "SELECT * FROM subcategories sc LEFT JOIN categories c ON sc.parent=c.catid WHERE sc.parent = 1"; |
|
#5
|
||||
|
||||
|
Quote:
I got only 1 row and all of the results in it where NULL ... but I am using MySQL, not MS SQL ... and I'm tired so that's why I posted it in the wrong place! Right post - Right Forum - http://forums.devshed.com/showthrea...1900#post691900 |
|
#6
|
||||
|
||||
|
Quote:
I get 0 rows returned ... |
|
#7
|
|||
|
|||
|
Code:
$query = SELECT * FROM categories a, subcategories b WHERE a.catid = b.catid AND Parent = 1 Can't see why you need to bother with all the JOIN commands for this simple sql when a simple Carthesian works fine. |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Using Joins - Why aren't them working? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|