473,666 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_linkcategori es 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_linkcategori es, and the link_category of table wp_links.

So in table wp_linkcategori es 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_linkcategori es 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 1928
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_linkcategori es;
+--------+----------+
| 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_linkcategori es 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_linkcategori es 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************ ********@o13g20 00cwo.googlegro ups.com> from justbn
contained the following:
select cat_id, cat_name, link_category, link_name from
wp_linkcategor ies 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_linkcategori es,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************ *********@dread er4.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
2363
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 on-the-fly instead of using 24 different ones. I had in mind using a case statement, something like this: select * from case when <input parameter> = 'something1' then tblSomething1 case when <input parameter> = 'something2' then tblSomething2...
1
1172
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 table with all the To locations and all the From Locations and their miles apart and then in a form be able to select a To Location and a From Location in two different dropdown boxes and have the miles show up the the field miles? I also want...
4
4719
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 athletes.  I have now learned how to join two different tables in a query so that I might generate a report. Specifically, student athletes at a community college are required to graduate with an AA or AS degree.  Consequently, various steps are in...
6
2054
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 need to use parts of the tables in datagrids, and here is where my problem lies
1
2912
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 have written the code using radio button for selecting single item.but we want to replace it with checkbox to select multiple items. the code using radio button is given below .pls correct it with checkbox
12
1301
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 don't know the names of any tables. Any help on how to solve this would be greatly appreciated! --
11
3445
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 than modified date, all my filters are selected via combo boxes. I would like to have the combo boxes update so that if there are no items available in the currently filtered list for a given selection, that selection will not appear in the...
48
3850
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 query of all students in both tables with no duplicates. No clue whatsoever.
6
3137
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 identifier, I would like a dropdown combo box (lookup from the "order_type" table) to change the type of the order. I also need an update command button, a delete row button and also an insert new row button. I'm sure this is a very common design...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.