开发者

how to select rows from table

开发者 https://www.devze.com 2023-01-03 01:08 出处:网络
my table contains category_name and parent_category_Id column My parent_category data contains, the same table primary key id.

my table contains category_name and parent_category_Id column

My parent_category data contains, the same table primary key id.

i need to select the all rows and insted my parent_category_Id i need to sel开发者_运维百科ect the catogry_name


I think this is what you're after, though it's hard to discern from the question:

Select c.*
From category c 
  Join parent_category pc ON c.parent_category_id = pc.id
Where pc.category_name = 'Some Name'


Try something like:

 SELECT c.category_name, p.category_name
  FROM categories c LEFT JOIN parent_categories p
  ON c.parent_id = p.id    

PS: you may think about restructuring your database, it would make more sense to store all the categories in the same table. See for instance: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/


You should restructure your database to have one table with:

id, name, and parent columns, with the parent column referencing the same table's id column. Your current database is not normalized and will likely cause issues in the future.

At a minimum you should have an auto_increment id column in the categories table.

The other answers here are correct (depending on the SQL server you are using).

0

精彩评论

暂无评论...
验证码 换一张
取 消