Forum Moderators: coopster
Anyway, it's a simple enough join question, but I've never understood them. MANY years ago, someone showed me how to do this without joins, and I would be much appreciative if someone could show me again ;^)
Ok, this is what I have (2 tables):
"tbl_links"
with fields: link_id, link_url, link_descr, link_cat_fk
(the last one is a link to the cat_id of the cat' table)
"tbl_category"
with fields: cat_id, cat_title, cat_desc
I want to display just the list of links, sorted by categories, but I want the categories to be in alphabetical order.
Seperately the SELECTS are:
"SELECT cat_id FROM tbl_category ORDER BY cat_title"
and
"SELECT * FROM tbl_links ORDER_BY link_cat_fk" <- not in alphabetical order, but sorted by groups
Now there was a way of micing these two quesries together to get the result. Can anyone help?
Thanks
english
-----------
¦nbr¦value¦
-----------
¦ 1 ¦ one¦
¦ 2 ¦ two¦
¦ 3 ¦three¦
-----------
french
------------
¦nbr¦ value¦
------------
¦ 2 ¦ deux¦
¦ 3 ¦ trois¦
¦ 4 ¦quatre¦
-----------
SELECT * FROM english, french;
----------------------
¦nbr¦ value¦nbr¦value¦
----------------------
¦ 2 ¦ deux¦ 1 ¦ one¦
¦ 3 ¦ trois¦ 1 ¦ one¦
¦ 4 ¦quatre¦ 1 ¦ one¦
¦ 2 ¦ deux¦ 2 ¦ two¦
¦ 3 ¦ trois¦ 2 ¦ two¦
¦ 4 ¦quatre¦ 2 ¦ two¦
¦ 2 ¦ deux¦ 3 ¦three¦
¦ 3 ¦ trois¦ 3 ¦three¦
¦ 4 ¦quatre¦ 3 ¦three¦
----------------------
SELECT * FROM english, french WHERE english.nbr = french.nbr;
----------------------
¦nbr¦ value¦nbr¦value¦
----------------------
¦ 2 ¦ deux¦ 2 ¦ two¦
¦ 3 ¦ trois¦ 3 ¦three¦
----------------------
Is this what you are referring to? If not, we'll take a look at subqueries, which are only available in MySQL version 4.1 and later.
SELECT *
FROM tbl_links, tbl_category
WHERE tbl_category.cat_id = tbl_links.link_cat_fk
ORDER BY cat_title, link_desc1"
This does exactly what I was after. I can't think of the eact query I needed to perform once before, but it involved several SELECTS nested within one another, the result of one SELECT would feed the parent one. Sounds a bit like what you wee going to offer with the sub-queries.
Can you enlighten me, and give some example?
Regards Dave
As I stated earlier, starting with version 4.1, MySQL supports all subquery [mysql.com] forms and operations which the SQL standard requires, as well as a few features which are MySQL-specific. If you were going to rewrite your query with a subquery statement in MySQL, first you are going to have to check your version:
SELECT VERSION();
+------------------------+
¦ version() ¦
+------------------------+
¦ 4.1.1a-alpha-max-debug ¦
+------------------------+
1 row in set (0.09 sec)
There are quite a few examples in the links provided. Personally, I'd stick with the JOIN you have. It's clean and easy to understand and efficient.