Recursive sql query for webpage URL [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm using postgresql but if there's a generic sql way even better.
I have a table storing data for webpages, including slug (e.g. in example.com/x/y, x and y are both slugs) and parent_id (x and y each have a record in this table. x's id matches y's parent_id).
If I have the id for y, is there an easy way to find out the full URL (/x/y)? Or at least all the id's (/x_id/y_id)?
I know that I can get this for this particular example like this:
select parent_id from pages where slug like 'x';
=> 5
And get the slug for that id:
select slug from pages where id = (select parent_id
from pages where slug like 'x');
=> y
But what if the URI was more like /a/b/c/d/e/f/g? Is there an easy way to find all of that if all I have is the id for g?

As #a_hourse-with_no_name said a recursive CTE should take care of you. This is my favorite link for CTE's: CTE Basics

Related

SQL LIKE doesn't find obvious matches [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm writing the following SQL query:
SELECT *
FROM OS
WHERE OS.VERSION LIKE '%1%';
In my table there are rows with char 1 in it. However, it returns an empty result.
I changed a little bit the LIKE clause to different values, but it still doesn't work.
What can I do to fix that?
Try double-quotes and * for wildcards. You are using Oracle syntax instead of Access syntax.
LIKE operation can't be used with columns of integer type. I assume that OS.Version is of integer type?
Edit1:
If you are referring to MS Access then you have to do the LIKE with stars (*) instead of %.

SQL server , Add data in multi value attribute [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table Photos with attribute comments
i want to declare that comments take many values
and want to know how to insert a new value to it
You should probably have a column photo_id in your comments table. This would let you have a lot of comments for each photo.
The way you are saying it, it would let you have the same comment in different photos, but that looks less likely to be what you really want.
Something like this:
photos:
id primary key
comments
id primary_key
photo_id foreign_key referencing photos(id)

ZF2 sql count rows with different values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm developing an application in zend framework 2 ans need to get some count values from a database table.
the table contains members with a column 'active' and 'blocked'
i would like te get the total number of members as well as the number of active and the number of blocked members.
Note that there will be a big amount of members in the table, so 'slow solutions' are not a option.
thanx for the help!
With those 2 columns, you would need to do something like:
SELECT SUM(active) as nrActive, SUM(if(blocked = 'yes', 1, 0)) as nrBlocked FROM members;
In this example "active" can have value 0 or 1, for blocked I assumed 'yes' and 'no', this way you have an example of both situations.
In ZF2 you can compose a query using Zend\Sql
$sql = new \Zend\Db\Sql($dbAdapter);
$sql->from('members', array(
'nrActive' => new \Zend\Db\Sql\Expression('SUM(active)'),
'nrBlocked' => new \Zend\Db\Sql\Expression('SUM(if(blocked = "yes", 1, 0))')
);
(disclaimer: I wrote this off hand so can have typo's, just for example)

Sql databases select command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am new to databases. And our teacher gave us pretty hard assignment. There are two tables. First table nickname is abilities(of superhero's:) ) and second table name superheros.
We have to select nick of Superhero and his average(medial) range for those who has two abilities?
Image of both tables:
Original here: http://postimg.org/image/85pqbc47n/
I will not give you solution - after all, it's homework and you have to learn something :) But I can give you an advice - try to do one task at a time
first, find those superheroes who has only 2 abilities (actually, you can do this by quering only table with abilities)
second - try to find average range of abilities for all superheroes (here you'll need join)
combine your queries
take a look at join, group by, count and having
Don't feel bad if you can't write it at first attempt, your query is not super easy, but 'm sure you can do this.
You can use HAVING and AVG() for this:
SELECT s.NickName, AVG(a.Range)
FROM abilities a
JOIN superhero s
ON a.ID_SuperHero = s.ID_SuperHero
GROUP BY s.NickName
HAVING COUNT(DISTINCT a.Abilities > 1)

Get me a SQL quesry to check whether the two table data contents are equal? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having two table A and B of same structure.Both tables is loaded with data.I just want the mismatched row of two tables.,,
I'm certain you could do some gigantic JOIN query for each column and then issue a NOT IN over the result but a simple, pragmatic solution is to:
Write a query that orders the data on several columns (or all)
Export the query executed on each table to a separate text file
Use diff to compare the files with each other
Depending on the RDBMS vendor you may have other options you could consider as well.