I have a MySQL table of following structure.
**Table elements :**
element_id element_name parent_id
1 UIG 0
2 CAM 1
3 IHG 1
4 USR 1
5 DBL 1
6 APD 1
7 RTM 1
8 OCR 2
9 IRT 3
10 ICR 3
11 OCR 2
12 USH 1
13 AML 1
I need to find child elements of a given element.
I made the following query :
SELECT parent_id,GROUP_CONCAT(element_id)
FROM elements
WHERE parent_id='1'
GROUP BY parent_id
which returns,
+-----------+--------------------------+
| parent_id | GROUP_CONCAT(element_id) |
+-----------+--------------------------+
| 1 | 2,3,4,5,6,7,12,13 |
+-----------+--------------------------+
1 row in set (0.00 sec)
While I need also need the childs of element 2 and 3, which should result into
+-----------+------------------------------------+
| parent_id | GROUP_CONCAT(element_id) |
+-----------+------------------------------------+
| 1 | 2,3,4,5,6,7,8,9,10,11,12,13 |
+-----------+------------------------------------+
1 row in set (0.00 sec)
How do I achieve this without procedures and just a query?
You just need to use the in operator on a subquery.
select group_concat(element_id)
from chpr
where parent_id in (
select group_concat(element_id)
from chpr
where parent_id = 1
group by parent_id)
;
** AS per OP's comment the levels are definitely a concern**
However based on the initial sample data and request, here is the SQLFIDDLE DEMO that provides the results as per the question's expected output.
The only change is that one needs to group by both element_id and parent_id the innner most first subquery.
Not a very elegant query at all:
select 1 as parent_id, group_concat(x.element_id)
from (
(select element_id
from chpr
where parent_id in
(select element_id
from chpr
where parent_id = 1
group by element_id, parent_id
))
union all
(select element_id
from chpr
where parent_id = 1
group by element_id, parent_id
)) x
;
Results:
PARENT_ID GROUP_CONCAT(X.ELEMENT_ID)
1 8,9,10,11,2,3,4,5,6,7,12,13
Use an IN () predicate...
SELECT '1' As Parent_Id,GROUP_CONCAT(element_id)
FROM elements
WHERE Parent_Id IN ('1','2','3')
GROUP BY '1';
Some database require the GROUP BY in this scenario others don't but it's fairly good practice to include it even if it is not needed.
Incidentally, I suspect Parent_Id is a numeric of some sort in which case it should really read...
SELECT 1 As Parent_Id,GROUP_CONCAT(element_id)
FROM elements
WHERE Parent_Id IN (1,2,3)
GROUP BY 1;
You could actually exclude the 1 As Parent_Id and GROUP BY 1 completely.
If you can only specify one value use a sub-query...
SELECT GROUP_CONCAT(element_id)
FROM Elements
WHERE Parent_Id IN (SELECT Element_Id
FROM Elements
WHERE Parent_Id = 1)
Take a look at my this question
Finding all parents in mysql table with single query (Recursive Query)
And here is the source for this kind of work.
http://explainextended.com/2009/07/20/hierarchical-data-in-mysql-parents-and-children-in-one-query/
Related
I have table:
id | parent | regno | person
1 | 0 | 12 | 5
2 | 1 | 12 | 15
3 | 0 | 13 | 5
4 | 0 | 14 | 6
I have MySQL query...
SELECT *
FROM table
WHERE person='5';
...that returns rows 1 and 3.
In this table row 1 and 2 are related (same regno).
How can i build this query to include related rows?
Basically when searching for person 5 i need MySQL query to return following:
id | parent | regno | person
1 | 0 | 12 | 5
2 | 1 | 12 | 15
3 | 0 | 13 | 5
Parent column has id of column it is related to, but it can be positive and negative integer. All related rows always have same regno.
Thank you.
You want all people who have a regno that is the same as the regno of anyone who is person 5:
--this main query finds all people with the regno from the subquery
SELECT *
FROM table
WHERE regno IN
( --this subquery finds the list of regno
SELECT regno
FROM table
WHERE person = '5'
)
There are other ways to write this; i'm not a fan of IN, and personally would write it like this:
SELECT t.*
FROM table t
INNER JOIN
(
SELECT DISTINCT regno
FROM table
WHERE person = '5'
) u
WHERE t.regno = u.regno
But it's harder to understand, and it's quite likely that these queries would end up being executed identically internally anyway. In this form the DISTINCT is required to make the regno from the subquery unique. If it were not, joined rows would end up duplicated. Why do I prefer it over IN? In some database systems IN's implementation can be very naive and low performing. "Never use IN to create a list longer then you would write by hand" is an old mantra I tend to stick to. This join pattern is also more flexible, can work with multiple values. Not every database supports Oracle-esque where x,y in ((1,3),(3,4)) value multiples
As an aside (and partly in response to the first comment on this answer) it would be more typical and more useful/usual to have the database prepare a set of rows that had parent and child data on the same line
It would look more like this:
SELECT *
FROM
table c
LEFT OUTER JOIN
table p
ON c.regno = p.regno AND p.parent = 1
WHERE c.person = '5' AND c.parent=0
This is assuming your "parent" column is 0 1 indicating true false.. you seem to have made a comment that parent is the id of the relative (not sure if it's parent-of or parent-is)
For a table where there is an id, and parentid column, and the parentid is set to a value when the row is a child of that other id;
id, parentid, name
1, null, Daddy
2, 1, Little Jonny
3, 1, Little Sarah
That looks like:
SELECT *
FROM
table c
INNER JOIN
table p
ON c.parentid = p.id
WHERE p.parentid ID NULL
Rows can have only one parent. A NULL in the parent id defines the row as being a parent, otherwise it's a child. You could turn this logic on its head if you wanted, call the column isparentof and have all child rows with null in the isparentof, and anyone who is a parent of a child, out the child id in isparentof. This then limits you to one child per multiple parents (single child families).. the query to pull them out is broadly the same
You can get all the id values for the person = '5' in a Derived Table.
Now, join back to the main table, matching either the absolute of parent (to get the child row(s)) or the id (to get the parent id row itself).
Based on discussion in comments, Try:
SELECT t.*
FROM your_table AS t
JOIN
(
SELECT id AS parent_id
FROM your_table
WHERE person = '5'
) AS dt
ON dt.parent_id = ABS(t.parent) OR
dt.parent_id = t.id
It is hard to comprehend though, why would you put negative values in parent!
So I have a student_profiles table and ranks table, I want to get the next rank based on the student rank. For example, I have rank 5 then the next rank will be rank 6. So this is my rank structure.
RANKS TABLE:
SELECT * FROM RANKS WHERE style_id = 1"
id style_id level name type primary_colour secondary_colour
1 1 1 Newbie double #4e90b2 #3aad04
22 1 2 Normal solid #fba729 NULL
31 1 3 Expert solid #4e805b NULL
and this is STUDENT_PROFILES TABLE
id | student_id | rank_id
------------------------------------
1 | 1 | 36
2 | 4 | 22
3 | 7 | 10
so all I have a variable is student_id, rank_id & style_id
so for example, I have this value student_id = 4, rank_id = 22 & style_id = 1
It should return
id style_id level name type primary_colour secondary_colour
31 | 1 | 3 | Expert | Solid | #4e805b | NULL
If you just want to get the second row:
Do it like this:
select * from
(select * from table order by id asc limit 2) as a order by id desc limit 1
Any query structure it will work as you need second row if you follow that script.
Try with that:
SELECT * FROM `ranks` WHERE `level` > (SELECT `level` FROM `ranks` WHERE `id` = rank_id) LIMIT 1
But I think it isn't very effective solution.
One option for getting the next highest level in the RANKS table is to self-join this table on the level column, order ascending, and retain the very first record only.
SELECT r2.*
FROM RANKS r1
INNER JOIN
STUDENT_PROFILES s1
ON r1.id = s1.rank_id
INNER JOIN
RANKS r2
ON r2.level > r1.level
ORDER BY r2.level
LIMIT 1
Demo here:
SQLFiddle
Note: If RANKS has duplicate levels, and you want the next level with regard to cardinality (i.e. you don't want a duplicate equal level returned), then my query could be slightly modified to filter out such duplicates.
i have searched for questions like this and although many are similars, are not answering exact my questions and queries not work.
Assuming we have the following table
id category_name parent_id
------------------------------------
1 test 0
2 test1 0
3 new_cat 1
4 new_catx 2
5 cat5 1
I want an sql query where the output will be like this
id category_name parent_id
------------------------------------
1 test 0
3 new_cat 1
5 cat5 1
2 test1 0
4 new_catx 2
In sort the output query is sorted based on parent_id. The parent_id = 0 is the root category, then child are following, and then again another parent with it's child. etc
This will work for a 1-level tree, i.e. for a tree containing only parents and their children:
SELECT *
FROM mytable
ORDER BY CONCAT(IF(parent_id=0, '', parent_id), id)
Demo here
Ideally you should use a mapping table for this, to make your solution cleanly scalable. Change your original table struct to
id category_name
---------------------
1 test
2 test1
3 new_cat
4 new_catx
5 cat5
...and have a parent mapping table, like so:
id parent_id
---------------------
3 1
4 2
5 1
Then you would simply modify Giorgos Betsos' excellent query to read:
select t.id, t.category_name, p.parent_id
from testtable t left outer join parents p
on t.id = p.id
ORDER BY CONCAT(IF(p.parent_id is null, '', p.parent_id), t.id);
It's hard to put in correctly, but I'm using MySQL and I need to select one id, let's call it parent_id which has to meet child_id values in multiple rows.
For example:
+-----------+----------+
| parent_id | child_id |
+-----------+----------+
| 1 | 10 |
+-----------+----------+
| 2 | 11 |
+-----------+----------+
| 2 | 12 |
+-----------+----------+
| 3 | 13 |
+-----------+----------+
| 4 | 11 |
+-----------+----------+
| 5 | 12 |
+-----------+----------+
Now if I pass child_id parameters 11 and 12, I have to get parent_id 2 back, but if I pass 10 and 11, I have to get nothing back. Also, if I pass 11, I have to receive 4. And if I pass 13, I have to receive 3 back.
How do I go about this? I tried counting the parent_id's and using HAVING clause, also using GROUP BY clause, but nothing I try meets all of my requirements.
EDIT:
Example Fiddle: http://sqlfiddle.com/#!2/abbc4/5
EDIT2:
Expected results:
Passed parameters: 11, 12
Received result: 2
Passed parameters: 11
Received result: 4
Passed parameters: 13
Received result: 3
Passed parameters: 12, 13
Received result NULL
EDIT3:
Updated the spec. See here also: http://sqlfiddle.com/#!2/2f750/1
The following statement does what you want. I am not so sure about its performance though...
select t.parent_id, t.cnt from
(
select parent_id, count(*) cnt
from t
WHERE child_id IN (12, 11)
GROUP BY parent_id
) t
inner join
(
select parent_id, count(*) cnt
from t group by parent_id
) s
on t.parent_id = s.parent_id
and t.cnt = s.cnt -- Check that the parent has exactly as many children as
-- passed in - and not more. Prevents matching if only part
-- of the children of a parent were specified.
and t.cnt = 2 -- Check that all passed in children produced a match on the same
-- parent. Prevents matching of parents that match only a subset
-- of the specified children
Replace the 2 with the number of specified children in the IN list.
You can also use this more compact version
select case
when min(t.parent_id) = max(t.parent_id) -- parent_ids are the same?
-- and all children share the same parent?
and count(t.parent_id) = (
select count(parents.parent_id)
from t parents
where parents.parent_id in
(select parent_id
from t
where child_id in (11, 12) -- <= change here
))
then t.parent_id
else null
end as parent_id
from t
where child_id in (11, 12); -- <= and here
I have tested this and works for all your use cases
You can test here http://sqlfiddle.com/#!2/abbc4/183
You have to have two variables for this to work. First one is the comma separated list of your child_ids (child_list) and second is the number of the children (the count of children in your child_list) you are searching for (child_count).
SELECT parent_id,COUNT(*)
FROM table
WHERE child_id IN (<child_list>)
GROUP BY parent_id
HAVING COUNT(*)=<child_count>
This should give you the desired results.
Editted heavily!
The original question was based on a misunderstanding of how IN() treats a column from a results set from a join. I thought IN( some_join.some_column ) would treat a results column as a list and loop through each row in place. It turns out it only looks at the first row.
So, the adapted question: Is there anything in MySQL that can loop through a column of results from a join from a WHERE clause?
Here's the super-simplified code I'm working with, stripped down from a complex crm search function. The left join and general idea are relics from that query. So for this query, it has to be an exclusive search - finding people with ALL specified tags, not just any.
First the DB
Table 1: Person
+----+------+
| id | name |
+----+------+
| 1 | Bob |
| 2 | Jill |
+----+------+
Table 2: Tag
+-----------+--------+
| person_id | tag_id |
+-----------+--------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 2 | 3 |
+-----------+--------+
Nice and simple. So, naturally:
SELECT name, GROUP_CONCAT(tag.tag_id) FROM person LEFT JOIN tag ON person.id = tag.person_id GROUP BY name;
+------+--------------------------+
| name | GROUP_CONCAT(tag.tag_id) |
+------+--------------------------+
| Bob | 1,2 |
| Jill | 2,3 |
+------+--------------------------+
So far so good. So what I'm looking for is something that would find only Bob in the first case and only Jill in the second - without using HAVING COUNT(DISTINCT ...) because that doesn't work in the broader query (there's a seperate tags inheritance cache and a ton of other stuff).
Here's my original sample queries - based on the false idea that IN() would loop through all rows at once.
SELECT DISTINCT name FROM person LEFT JOIN tag ON person.id = tag.person_id
WHERE ( ( 1 IN (tag.tag_id) ) AND ( 2 IN (tag.tag_id) ) );
Empty set (0.00 sec)
SELECT DISTINCT name FROM person LEFT JOIN tag ON person.id = tag.person_id
WHERE ( ( 2 IN (tag.tag_id) ) AND ( 3 IN (tag.tag_id) ) );
Empty set (0.00 sec)
Here's my new latest failed attempt to give an idea of what I'm aiming for...
SELECT name, GROUP_CONCAT(tag.tag_id) FROM person LEFT JOIN tag ON person.id = tag.person_id
GROUP BY person.id HAVING ( ( 1 IN (GROUP_CONCAT(tag.tag_id) ) ) ) AND ( 2 IN (GROUP_CONCAT(tag.tag_id)) );
Empty set (0.00 sec)
So it seems it's taking a GROUP_CONCAT string, of either 1,2 or 2,3, and is treating it as a single entity rather than an expression list. Is there any way to turn a grouped column into an expression list that IN () or =ANY() will treat as a list?
Essentially, I'm trying to make IN() loop iteratively over something that resembles an array or a dynamic expression list, which contains all the rows of data that come from a join.
Think about what your code is doing logically:
( 1 IN (tag.tag_id) ) AND ( 2 IN (tag.tag_id) )
is equivalent to
( 1 = (tag.tag_id) ) AND (2 = (tag.tag_id) )
There's no way tag.tag_id can satisfy both conditions at the same time, so the AND is never true.
It looks like the OR version you cited in your question is the one you really want:
SELECT DISTINCT name FROM person LEFT JOIN tag ON person.id = tag.person_id
WHERE ( ( 1 IN (tag.tag_id) ) OR ( 2 IN (tag.tag_id) ) );
Using the IN clause more appropriately, you could write that as:
SELECT DISTINCT name FROM person LEFT JOIN tag ON person.id = tag.person_id
WHERE tag.tag_id in (1,2);
One final note, because you're referencing a column from the LEFT JOINed table in your WHERE clause (tag.tag_id), you're really forcing that to behave like an INNER JOIN. To truly get a LEFT JOIN, you'd need to move the criteria out of the WHERE and make it part of the JOIN conditions instead:
SELECT DISTINCT name FROM person LEFT JOIN tag ON person.id = tag.person_id
AND tag.tag_id in (1,2);
WHERE ( ( 1 IN (tag.tag_id) ) AND ( 2 IN (tag.tag_id) ) );
This will never return any results since tag.tag_id cannot be 1 and 2 at the same time.
Additionally is there a reason you're using 1 IN (blah) rather than blah = 1?