473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

selecting data from two different tables

Hello.

I am just wondering if someone can help me with my PHP/MySql code as I
am not too proficient with it yet.

What I am wanting to do, is display data from two tables.

The first table is called wp_linkcategories and has a list of category
names for my different links.

The second table is called wp_links and is where the actual links are
stored.

I would like to have the category name displayed from the first table
and then all the links that fall in that category listed underneath
from the second table, and then the next category link displayed and
its links until all the category names and links have been displayed.

I believe I need to do an inner join between the two tables and have a
repeatable region until all the data is displayed?

The only items in the two tables that are the same are cat_id of table
wp_linkcategories, and the link_category of table wp_links.

So in table wp_linkcategories I have cat_id and cat_name which I would
need.

In table wp_links I have link_category, link_name, link_url and
link_rating.

I would like the cat_name from table wp_linkcategories listed by
cat_id descending.

Following that I would like to have the link_name from table wp_links
along with the url to make it a hyper link listed by link_rating
descending.

Any ideas how to go about this? I am a bit befuddled on where to
start as I've not done anything this complicated with PHP before.

John
Jul 17 '05 #1
4 1912
John,

You don't need to do any nested loops to accomplish this. The trick
can be done with the SQL statement. I built some simple tables that
represent your data definition. Here they are :

-----------------------------------------------
mysql> select * from wp_linkcategories;
+--------+----------+
| cat_id | cat_name |
+--------+----------+
| 1 | cats |
| 2 | dogs |
| 3 | birds |
| 4 | horses |
+--------+----------+
4 rows in set (0.00 sec)

-----------------------------------------------
mysql> select * from wp_links;
+---------------+-----------+
| link_category | link_name |
+---------------+-----------+
| cats | siamese |
| horse | shetland |
| cats | manx |
| horses | mule |
| cats | tabby |
| dogs | chow chow |
| birds | cockateel |
| dogs | shepherd |
| birds | parrot |
| dogs | hound |
| cats | bobcat |
| horses | palamino |
+---------------+-----------+
12 rows in set (0.00 sec)
-----------------------------------------------

Now, here is the SQL statement to use :

select cat_id, cat_name, link_category, link_name from
wp_linkcategories left join wp_links on cat_name = link_category order
by cat_id desc;

-----------------------------------------------

Here are the results :

mysql> select cat_id, cat_name, link_category, link_name from
wp_linkcategories left join wp_links on cat_name = link_category order
by cat_id desc;
+--------+----------+---------------+-----------+
| cat_id | cat_name | link_category | link_name |
+--------+----------+---------------+-----------+
| 4 | horses | horses | palamino |
| 4 | horses | horses | mule |
| 3 | birds | birds | cockateel |
| 3 | birds | birds | parrot |
| 2 | dogs | dogs | shepherd |
| 2 | dogs | dogs | chow chow |
| 2 | dogs | dogs | hound |
| 1 | cats | cats | siamese |
| 1 | cats | cats | tabby |
| 1 | cats | cats | manx |
| 1 | cats | cats | bobcat |
+--------+----------+---------------+-----------+
11 rows in set (0.00 sec)


I hope this helped.
justbn

Jul 17 '05 #2
I noticed that Message-ID:
<11********************@o13g2000cwo.googlegroups.c om> from justbn
contained the following:
select cat_id, cat_name, link_category, link_name from
wp_linkcategories left join wp_links on cat_name = link_category order
by cat_id desc;


I suspect this has been done before, but why is left join better than
where?

eg
select cat_id, cat_name, link_category, link_name from
wp_linkcategories,wp_links where cat_name = link_category order
by cat_id desc;

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
Geoff Berrow wrote:
I suspect this has been done before, but why is left join better than
where?


It's different; using where is the same as inner join. Left join
includes all records from the first table, even if there's no match in
the second table.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4
I noticed that Message-ID:
<42*********************@dreader4.news.xs4all.nl > from Ewoud Dronkert
contained the following:
I suspect this has been done before, but why is left join better than
where?


It's different; using where is the same as inner join. Left join
includes all records from the first table, even if there's no match in
the second table.


Ah..thanks.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: manning_news | last post by:
Using SQL2000. How do I format my select statement to choose one out of 24 different tables? Each table is slightly different and I was hoping I could use one select statement and format it...
1
by: Sean | last post by:
I am using Access 2002. I would like to track mileage between buildings. I work for a school district so for Example I went from the High School to my Office - 3.2 Miles away. Can I set up a big...
4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
12
by: Lasse Eskildsen | last post by:
Hello, I would like to load various access databases in my application, but I can't figure out how to get all tabels from the database into my dataset. The databases are all different, so I...
11
by: Ron L | last post by:
I have a data table that lists a series of items in my database. In my user form, I want the user to be able to filter by a number of criteria (e.g. location, contract, date modified, etc). Other...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
6
by: Mike Wilson | last post by:
Dear Group, I have a heirarchical set of database tables, say - "order" and "order_type" and want to display a series of orders in a grid control, and in place of the order_type foreign key...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.