Jump to content

Sorting by values from linked table


neocorps

Recommended Posts

Hello!

 

Im new in this forum and i hope you can help me with my question.

 

Basically i have 2 tables, one called categories and the other one empresas.

 

categories has ID and Category(category names), empresas has many more but im only interested in ID_CATEGORY(linked id from Categories).

 

What my php does, is that it prints all of the data from empresas, but when i want to sort them by category, it sorts by the ID from Categories, and i need to sort  them using the category names.

 

This is the code i have for sorting it.

 


//obtain the sorting order
if (isset($_GET['order'])){
 $order = $_GET['order'];
}else{
 $order = "name";
}

$query  = "SELECT * FROM empresas ORDER BY $order LIMIT $offset, $rowsPerPage";

 

nevermind the $offset, and $rowsPerPage, its just for paging.

 

So can you help me with this?

Link to comment
https://forums.phpfreaks.com/topic/138254-sorting-by-values-from-linked-table/
Share on other sites

First of all, if you are using LIMIT, you must be using MySQL...not MSSQL. So, can a mod please move this?

 

To answer your question, you need to join the tables:

 

SELECT * FROM empresas e LEFT JOIN categories c ON e.id_category = c.id ORDER BY c.category

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.