I need one help.I need to fetch value from database by joining two table and that will be in a given order. I am explaining table below.
db_cat:
cat_id cat_name order
1 food 1
2 Drink 2
3 Ice 3
db_subcat:
subcat_id subcat_name cat_id order
1 pizza 1 1
2 chiness 1 3
3 Stampede Party 1 2
4 Wine 2 1
5 HAPPY HOUR 3 1
6 BS 1 4
my expected output is given below.
pizza, Stampede Party, chiness,BS , Wine , HAPPY HOUR
From the above two table i need all subcat_name
and that is only as per db_cat
order
column and db_subcat
order
column.Here the subcat_name under food should come first and that should also follow the db_subcat
order column and so on.Please help me to write this query.
you use ORDER BY
to order the columns, you can use multiple columns in an order by
Read here for more info
SELECT subcat_name
FROM db_subcat
JOIN db_cat ON db_cat.cat_id = db_subcat.cat_id
ORDER BY db_cat.order DESC, db_subcat.order DESC
This is pretty basic, you should read up more on mysql and find examples to learn from.
Use ORDER BY
and try below query. It will first set order by with db_cat orderby field and then order by with db_subcat order by field.
ASC
for ascending order and DESC
for sescending order.
SELECT ds.subcat_name FROM `db_subcat` as ds
JOIN db_cat as d ON d.cat_id=ds.cat_id
ORDER BY d.orderby,ds.orderby ASC;
以下是解决方案:
SELECT * FROM
db_subcat
AS sc LEFT JOIN db_cat
AS c ON sc. cat_id
= c. cat_id
ORDER BY c.order ASC, sc.order ASC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.