Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)

You can do it in a single call from php to mysql if you use a stored procedure: Example calls mysql> call category_hier(1); +——–+—————+—————+———————-+——-+ | cat_id | category_name | parent_cat_id | parent_category_name | depth | +——–+—————+—————+———————-+——-+ | 1 | Location | NULL | NULL | 0 | | 3 | USA | 1 | Location … Read more

SQL – CTE vs VIEW

Views can be indexed but CTE can’t. So this is one important point. CTE work excellent on tree hierarchyi.e. recursive Also, consider views when dealing with complex queries. Views being a physical object on database (but does not store data physically) and can be used on multiple queries, thus provide flexibility and centralized approach. CTE, … Read more

SQL Server query with pagination and count

Assuming you are using MSSQL 2012, you can use Offset and Fetch which cleans up server-side paging greatly. We’ve found performance is fine, and in most cases better. As far as getting the total column count, just use the window function below inline…it will not include the limits imposed by ‘offset’ and ‘fetch’. For Row_Number, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)