Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

sql recursive menu

Posted on 2012-08-23
3
Medium Priority
?
960 Views
Last Modified: 2012-08-23
hey guys i have a table and doing a search from the menu table

here the coloums

id                 parent_ id                  title                 url
1                        0                           Products          
2                        1                           Shoes
3                        2                           Size 5              ~/#

if i enter Shoes to search it must list the child of shoes which is size 5 and url is not null

please help me with the query
0
Comment
Question by:JCWEBHOST
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38324393
this should do:
;with data as (
  select id, parent_id, title, url
    from yourtable t
  where t.title = 'Shoes'
 UNION ALL
  select t.id, t.parent_id, t.title, t.url
    from data
    join yourtable t
      on t.parent_id = data.id 
)
select * from data where url is not null

Open in new window

0
 
LVL 7

Expert Comment

by:aplusexpert
ID: 38324452
Try this query it will help you

select * from menu
where title = 'Shoes'
union
select * from menu
where parent_id = (select id from menu
					where title = 'Shoes')
	and url IS NOT NULL

Open in new window


Thanks...
0
 

Author Closing Comment

by:JCWEBHOST
ID: 38324484
works fine
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question