Any better way to select like this?

Hi,

I have a category_table with the fields like below:

id
category_name
parent

To get the Category Name and its parent name, we can use the simple but bad way like below:

$query=mysql_query(“Select * from category_table where category_name like ‘%tech%’”);
while($row=mysql_fetch_array($query)){

$query2=mysql_query(““Select * from category_table where parent ='”.$row[‘id’].”'");

}

May I know how to get the category name with its parent name in one query?

SELECT
  t1.id,
  t1.category_name AS `category_name`,
  t2.category_name AS `parent_name`
FROM
  category_table t1
LEFT OUTER JOIN
  category_table t2
ON
  t1.parent = t2.id

Thanks Dan Grossman, I’ll do further study on this part!

Thanks for sharing!