M
M
Minningt2015-07-31 16:07:01
MySQL
Minningt, 2015-07-31 16:07:01

How to link 2 tables?

There is a Rayons table. It has fields id, rajon_id
And there is a table Bars. It contains the fields id, group_id, name
The task is to display a list of bars in the desired area., but you need to show not the id, but the name of the bar.
Rajons.rajon_id and Bars.id are related. Let me remind you once again that you need to display the name of the bar
Thank you for your attention

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Therapyx, 2015-07-31
@Minningt

Let's assume that the base will look a bit better and clearer:
-Rajon:
Rajon_ID; //primary
District_Name;
-Bar:
Bar_ID;
bar_name;
region_id; //foreign k Rajon.Rajon_ID;
then:

SELECT bar.Bar_Name FROM Bar, Rajoin WHERE Bar.Rajon_ID = Rajon.Rajon_ID AND Rajon.ID = "тут айди без скобок"

D
dk-web, 2015-07-31
@dk-web

$q="SELECT name FROM bars AS a INNER JOIN rajons as b ON b.rajon_id=a.id WHERE a.id=2";

$res=mysql_query($q) or die ("Ошибка БД” ".mysql_error());
while ($row=mysql_fetch_assoc($res)) {
  echo '<br>'.$row['name'];
}

a.id - desired area

D
Dmitry Kovalsky, 2015-07-31
@dmitryKovalskiy

well, 1) learn the zen of the JOIN word, 2) how do you want to associate a bar id with an area id, just like the hell knows why the area has an id and area_id . What is the difference?! And yes, learn English. Transliteration, except for turning the brain into a raskoryak, will not cause anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question