473,398 Members | 2,343 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,398 software developers and data experts.

mysql join

bilibytes
128 100+
hi all,

i am trying to get all the names of the restaurants in a city.

i have 3 tables:

1. countries -> list of countries in 4 languages (each country has the same id in the different languages)
id | country_id | name | lang

2. cities-> list of all cities in 4 languages (each city is associated with the country_id and has the same id for the different languages)
id | country_id | city_id | name | lang

3. restaurants-> list of all restaurants in the different countries (each restaurant has associated the country_id and the city_id)
id | country_id | city_id | name



ok what i want is to get all the names of the restaurants in a city
i did this:

i pass these two variables $country and $city

Expand|Select|Wrap|Line Numbers
  1. "SELECT co.country_id, ci.city_id, p.name
  2.         FROM countries co, cities ci, premises p
  3.         WHERE co.name = '$country' AND
  4.               ci.name = '$city' AND 
  5.               ci.country_id = co.country_id AND 
  6.               p.city_id = ci.city_id";
is this the right way to perform this?
i have to use all these ids because of the languages

should i use a subquery instead?

thankyou very much
Nov 2 '08 #1
2 1974
Atli
5,058 Expert 4TB
Hi.

What is the difference between the "id" and "country_id" field in the "country" table?
On the same note, what is the difference between "id" and "city_id" in the "City" table?
Why do you store both the "country_id" and "city_id" in the "restaurants" table?

If I needed to store a list of countries, and the names of those countries in multiple languages, I would use a structure like this:
Expand|Select|Wrap|Line Numbers
  1. Country
  2. -----------------
  3. CountryID Int PK
  4. CountryName VarChar(255) /* In English, as the default name */
  5. -----------------
  6.  
  7. Language
  8. -----------------
  9. LanguageID Int PK
  10. LanguageName VarChar(255) /* In English */
  11. LanguageLocale VarChar(255) /* The name of the language in that language */
  12. -----------------
  13.  
  14. CountryName
  15. -----------------
  16. NameID Int PK
  17. CountryID_FK Int References Country(CountryID)
  18. LanguageID_FK Int References Language(LanguageID)
  19. NameValue VarChar(255)
  20. ------------------
  21.  
Which would also work for the Cities and Restaurants, although you could reuse the "Language" table in those as well.
Nov 3 '08 #2
bilibytes
128 100+
Hi.

What is the difference between the "id" and "country_id" field in the "country" table?
On the same note, what is the difference between "id" and "city_id" in the "City" table?
Why do you store both the "country_id" and "city_id" in the "restaurants" table?

If I needed to store a list of countries, and the names of those countries in multiple languages, I would use a structure like this:
Expand|Select|Wrap|Line Numbers
  1. Country
  2. -----------------
  3. CountryID Int PK
  4. CountryName VarChar(255) /* In English, as the default name */
  5. -----------------
  6.  
  7. Language
  8. -----------------
  9. LanguageID Int PK
  10. LanguageName VarChar(255) /* In English */
  11. LanguageLocale VarChar(255) /* The name of the language in that language */
  12. -----------------
  13.  
  14. CountryName
  15. -----------------
  16. NameID Int PK
  17. CountryID_FK Int References Country(CountryID)
  18. LanguageID_FK Int References Language(LanguageID)
  19. NameValue VarChar(255)
  20. ------------------
  21.  
Which would also work for the Cities and Restaurants, although you could reuse the "Language" table in those as well.

ok, i'll trust in you. as i dont know what is resources costless.

you told me: why do u have a repeated id: id and country_id.

If i understood well, mysql always needs an key/index which should be a non repeated value, and as i have multiple languages i need an id to associate the countries in turkish to the countries i understand -> in english... that is why country_id would be a repeated value.
As i need an index which is non-repeated value, i added id which is key auto increment.

your table structure doesnt need this repetition, that's why i think yours should be better.

So thankyou very much!!

by the way, isn't there a web page where i can get all countries almost ready to be inserted to my table?
Nov 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream...
6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
23
by: phpfrizzle | last post by:
Hi there, I have a site with products on it. The site has a mysql backend. All products belong to certain series (table series). There can be up to 4 different products (table products)...
0
by: C. Reeve | last post by:
Hi, Not sure if the problem here is PHP or MySQL, but here we go. I am trying to do two queries on a database - one after the other, but the second one never seems to get executed. The two...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Phil Powell | last post by:
I have a rather complicated query with a combination of LEFT JOINs and two MATCHES where the first match is non-boolean to get the accurate score, the second to search as boolean: SELECT...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
0
by: yeahuh | last post by:
Quick and dirty version. Godaddy server using MySQL 4.0.24 I’m trying a left join to obtain id’s in table A(cars) that are NOT in car_id in table B(newspaper): *This is a cut down version...
0
by: jllanten | last post by:
I will appreciate any help you can provide me. In the company where i work we have a project which creates about 4-5M records daily of stats. We're currently storing this data in a db named...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.