English 中文(简体)
• 将儿童类别列入父母名称,在我sql
原标题:getting child category s parent name with a single query in mysql
  • 时间:2012-05-03 07:51:10
  •  标签:
  • mysql

I have a mysql table named "category". the basic structure looks like this-

------     --------     ------
cat_id     cat_name     parent
------     --------     ------
1          test1        NULL
2          test2        NULL
3          test3        2
4          test4        1
5          test5        3

现在我想到与母类别有关的所有类别数据 姓名。 在单项询问中(而不仅仅是id)。 这是可能的。 i 可以通过使用第二个查询(带孩子的母姓)来做,同时使用整个循环和合并的数据。 但这能否用一个单一问题来做?

最佳回答

Join the table with itself, using the parent column to link to the cat_id of the parent.

SELECT c1.cat_id as childID, c1.cat_name ChildName, c2.cat_name as ParentName
from category c1
LEFT OUTER JOIN category c2
ON c1.parent = c2.cat_id
  • Be careful: since some elements have no parents (NULL), I put a LEFT OUTER JOIN so those rows are displayed as well. If you don t want that, use a JOIN instead of LEFT OUTER JOIN.
  • You can also show the lines, but display something else (empty or a text or ...) instead of the NULL by using COALESCE.
  • You can consider the result as one (big) new table, so you can add WHERE clauses as you usually do, for example filtering on the parent name: WHERE c2.cat_name = test2
问题回答
Select p.cat_id, p.cat_name, c.cat_id, c.cat_name, c.parent 
From category Left Join category On p.cat_id = c.parent 
Where p.cat_name =  name 

SELECT c1.category_name AS category, c2. category_name AS sub_category FROM (

SELECT * FROM category ) AS c1 INNER JOIN (

SELECT * FROM category ) AS c2 ON c1.category_id = c2.category_id





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签