Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old November 15th, 2001, 09:17 AM
Helge Helge is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Trondheim, Norway
Posts: 4 Helge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
MySql: select statement

Hei

I'm a newbie to mysql (and PHP) so hopfully this is the right place to post my question.

I'm trying to create a site about travelling, with different travelletters and infofmation about a country.
I have one table that stores information about the the different articles, one which stores the information about countries and the last one which stores information about the different regions (Europe, Asia, Africa....).

So this is the fields in the different tables:

article -> id, artname, url, countryid
country -> id, cname, regionid
region -> id rname
(The id's, countryid and regionid are all integers)

An article can only be assigned to one country, but a country can be assigned to many different articles.
A country can only be assigned to one region, but a region can be assigned to many different countries.

The problem is:
I'm trying to make the navigation system for my site. So when the user choose a region all the countries in the region are going to be displayed. So far I'm able to help myself. I use this code to that:
(the variable $rid is being passed from the link that the useres clicks on when they choose a region)
PHP Code:
 SELECT idcnavn FROM country WHERE region.id='$rid' 

This code displays all the countries wich are in the same region.

But I also want to do is this:
I only want to display the countries which have an article assigned to itself (This means that there can be countries in the table which are not assigned to an article). And I only want to display the country ones. So even if there's three articles about one country I only want the country to be displayed ones.
Could anyone help out whith a select statement that take care of this?

Hopefully I've made my explenation good enough.

-Helge

PS! I'm soory that my english aren't to good.

Last edited by Helge : November 15th, 2001 at 09:54 AM.

Reply With Quote
  #2  
Old November 15th, 2001, 11:02 AM
Fataqui Fataqui is offline
Senior Member
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: Boston Ma.
Posts: 1,530 Fataqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 42 sec
Reputation Power: 0
MYsql help....

Hi

I sort of understand what you are wanting..........


But to me I would do things a little different.......


you say you have 3 tables......

article

country

regions


The key to all of this seems (REGIONS).......


I would use only one table...........


regions

key_id = region id


Example....

region [1] = North America

region [2] = South America

region [3] = Europe

region [4] = Asia

region [5] = Africa



So a table could look like this......




$key_id', '$key_country', '$region_country', '$article', '$date_added', '$last_update'



your page (html) could then call a region...........


and send the first result................

Like..............

This......

user selects region[1]........


the database is called and outputs a list of "countries" that region contains...........

$sql = "SELECT key_id FROM regions WHERE (key_id = '$what_region_the_user_selected')";

From there the user selects a country, that was pulled from the last query.....


$sql = "SELECT key_id, key_country FROM regions WHERE (key_id = '$first form selection') AND (key_country = '$country_selected')";


From there the country will be displayed, and you can then display genral country info based on the $region_country or on the date it was add or updated........


To me I would rethink your database structure.......


Just my thoughts....

F!

Reply With Quote
  #3  
Old November 17th, 2001, 05:52 AM
Helge Helge is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Trondheim, Norway
Posts: 4 Helge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Thank you Fataqui for your reply.

I kind of understanding your suggestion, but I don't think I would like to redisgn my database. I do have a lot more coulms in each table than I wrote in the last post.

But I found a solution. It's not a good soultion cause I do some while-loops to many times, but I think it will work for now.

Here's my solution;
PHP Code:
 mysql_query("DROP TABLE IF EXISTS findcountry");
mysql_query("CREATE TEMPORARY TABLE findcountry (country_id INT, country_name VARCHAR(100) PRIMARY KEY)");

$countries mysql_query("SELECT id, cnavn FROM country WHERE regionid='$what_region_the_user_selected'");
    while (
$country mysql_fetch_array($countries)) {
        
$cid $country["id"];
        
$cname $country["cname"];

        
$countries2 mysql_query("SELECT countryid FROM article WHERE countryid=$cid");
            while (
$country2 mysql_fetch_array($countries2)) {
                
$countryid $country2["$countryid"];

                if(
$cid == $countryid) {
                    
$sql=mysql_query("INSERT IGNORE INTO findcountry SET country_id='$cid, country_name='$cname'");
                    } 
// End if($cid == $countryid)
                
// End while $country2
        
// End while $country

$countries3 mysql_query("SELECT country_id, country_name FROM findcountry ORDER BY country_name");
    while (
$country3 mysql_fetch_array($countries3)) {
        
$country_id $country3["country_id"];
        
$country_name $country3["country_name"];

        echo(
"$country_id - $country_name<br>\n");
        } 
// End while $country3 

Thanks again, Fataqui, for your reply.

-Helge

Last edited by Helge : November 17th, 2001 at 05:55 AM.

Reply With Quote
  #4  
Old November 17th, 2001, 08:07 AM
Humble Seeker Humble Seeker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: London
Posts: 69 Humble Seeker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 54 sec
Reputation Power: 7
Regardless of the previous solution I think you've done your database correctly, its the way I got taught to do it at Uni. Anyway to answer your first query, your query isn't working because you haven't referenced the region table in the from part, i.e.

SELECT country.id, country.cnavn FROM country, region WHERE region.id='$rid'

If I remember my PHP this should now work. If you have anymore problems I'd suggest using the actual PHP forum on Dev Shed as you'll probably get a better response.
__________________
Humble Seeker

The longest journey starts with the smallest step, and knowledge is the longest journey of all.

Reply With Quote
  #5  
Old November 17th, 2001, 09:05 AM
Helge Helge is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Trondheim, Norway
Posts: 4 Helge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Thank you. You're right.

Actually I see that I should not have referenced to the table region at all. This is what the statement should be:
PHP Code:
 SELECT*id,*cnavn*FROM*country*WHERE*regionid='$rid' 

I probaly move this topic to the PHP or MySql forums to get help with only displaying the countries which have an article assigned to itself.

-Helge

Reply With Quote
  #6  
Old November 17th, 2001, 09:23 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,632 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 12 m 33 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
Here's how I'd do the SQL. And your table design is correct, you've normalized your data, which is the proper way to do this, to avoid repetitious data, among other reasons.

Code:
select country.cname as countryname from country,article 
where 
    (country.id = article.countryid) 
group by countryname


or, to get the same and integrate the region select,

Code:
select country.cname as countryname from country,article 
where 
    (country.id = article.countryid) and 
    (country.regionid='$rid') 
group by countryname


or, if you can't pass the regionid, you can use the region name

Code:
select country.cname as countryname from country,article,region 
where 
    (country.id = article.countryid) and 
    (country.regionid=region.id) and 
    (region.rname='$rname') 
group by countryname


These would get the country name once for every country that has a country id in the article table. This is known as a join, the mySQL manual has an OK description of them.

Let the SQL engine do the work for you. As long as your column types and sizes are the same for the column you compare and your columns are indexed, this should be much faster, given that you can easily do this with one SQL statement.

Last edited by Hero Zzyzzx : November 17th, 2001 at 09:33 AM.

Reply With Quote
  #7  
Old November 18th, 2001, 05:47 AM
Helge Helge is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: Trondheim, Norway
Posts: 4 Helge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 sec
Reputation Power: 0
Thank you, Hero Zzyzzx!!

This was exactly what I was looking for. It works great!

-Helge

Reply With Quote
  #8  
Old November 18th, 2001, 02:20 PM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,632 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 12 m 33 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
No problem! Quite a bit easier and faster than all those loops you had, huh?

A I mentioned above, mySQL has a serious limitation with joins in that each column being compared has to be indexed and of the same type and size (e.g. all varchar(20) if that's how your doing them) otherwise mySQL won't use indeces, making your selects far slower.

You can "alter table" to fix them without worrying about losing data, one advantage that mySQL has over postgreSQL.

Good luck! Joins rock.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > MySql: Problems wtih a select statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway