I have a category table, and an s_category table. Each category can have many s_category for eg:
Table 1 : category
------------------------------ <br/> | id_category | name_category | | ---------------------------- | 1 | vehiculs | ------------------------------ <br/>
Table 2 : sous_category
----------------------------------------- <br/> | id_scategory | name_scategory | id_cat | | --------------------------------------- <br/> | 1 | cars | 1 | | ---------------------------------------- <br/> | 2 | motors | 1 | ------------------------------------------ <br/>
I'm using this:
SELECT category.name_category,sous_category.name_scategory
FROM category
JOIN sous_category
ON category.id_category=sous_category.id_cat ';
But the OUTPUT is duplicat the title of category :
array
0 =>
array
'name' => string 'vehiculs' (length=8)
'name_s' => string 'cars' (length=4)
1 =>
array
'name' => string 'vehiculs' (length=8)
'name_s' => string 'motors' (length=6)
2 =>
array
'name' => string 'vehiculs' (length=8)
'name_s' => string 'pieces' (length=6)
3 =>
array
'name' => string 'electronics' (length=11)
'name_s' => string 'tv' (length=2)
4 =>
array
'name' => string 'electronics' (length=11)
'name_s' => string 'mobile' (length=6)
I want to see only :
Vehicules :
Electronics :
to use it in top menuBar
That is what the result set from SQL looks like. There's no way around that. If you want to adjust it in PHP, here's how:
Assuming you want an array that looks like this:
array(
"Vehicules" => array(
"cars",
"motors",
"piece",
),
"Electronics" => array(
"TV",
"mobile",
),
)
You can walk the result set and build this array:
$final = array();
foreach($results as $row)
$final[$row["name"]][] = $row["name_s"];
You have a basic conceptual error here. MySQL implements a relational algebra .
It is an "algebra" in that it has a type, values of that type, and operators that take values of that type and produce other values of that type. So addition, subtraction, and multiplication create an algebra on integers, because adding, subtracting, and multiplying integers together only produces more integers. Division is not part of that algebra because the ratio of two integers is not necessarily an integer.
It is an "relational algebra" in that the type it works on is "relations". A relation can be thought of as a table where the columns are named and the rows are numbered:
+---------+-------------+--------+
| | name | name_s |
+---------+-------------+--------+
| 1 | vehicles | cars |
| 2 | vehicles | motors |
| 3 | vehicles | pieces |
| 4 | electronics | tv |
| 5 | electronics | mobile |
+---------+-------------+--------+
The important thing to remember: whatever you do, however you twist and squirm, the result of a SQL expression will be just another relation.
Your problem is, you want a structure that isn't a relation. It's a tree or a hierarchy or something like that, and SQL don't play that way.
If you want a tree, you have to create it in post-processing (in your case, with PHP code, and may God have mercy on your soul).
Two technical points for the compulsive among you:
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.