|
|
|
|
SQL and showing data from two tables
We will learn how we can write SQL command to show data from two tables and also specify some additional conditions to limit the amount of data returned.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.
|
|
|
Basic input for our query will be data introduced in lesson
If you are still learning SQL I really recommend to see this lesson.
A new field id_country was added to the table my_table.
|
|
|
2.
|
|
|
Now we create a new table country with fields id and name.
We enter a few test values into the table.
Main benefit of storing data into separate tables is that in case the name of the the country changes, it is enough to correct only one record in one table.
If the name of the country would be in the same table as other data, we would have to change all the records with that country.
|
|
|
3.
|
|
|
Data from two tables can be joined by setting condition in SQL where we define that field from one table should be the same as the field from other table.
In our case these fields are id_country and id.
SQL command is shown below:
SELECT * FROM my_table AS a, country AS b WHERE a.id_country = b.id
|
|
|
4.
|
|
|
As a result we now see fields from both tables joined into one table where values of id and id_country matches.
|
|
|
5.
|
|
|
Of course we usually do not need all the fields and instead of * we type the fields that we want.
SELECT a.text, b.name FROM my_table AS a, country AS b WHERE a.id_country = b.id
By doing this we also reduce memory consumption and speed up the execution of the query.
|
|
|
6.
|
|
|
As a result we now get only fields that we requested.
|
|
|
7.
|
|
|
Usually we also do not need all the records from the table, only the records which satisfy certain condition.
In this case we can add additional conditions at the end of SQL command.
If we want to see only records with county Japan, we can use the following SQL command:
SELECT a.text, b.name FROM my_table AS a, country AS b WHERE a.id_country = b.id AND b.name='Japan'
|
|
|
8.
|
|
|
As a result we now see only records with country Japan.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Cheap SMM Panel Social Media Marketing is a huge field with a lot of competition. If you want to stand out from the crowd, you need to know how to find ways to make your content go viral. If you are interested in using social media to market your product, this service is perfect for you. The panel gives you access to hundreds of different social media sites, so that you can find new ways to market your business. Visit our website at: https://thefansbuzz.com/ |
|
|
|
|
|
|
|
|
For people and businesses looking to improve their social media presence, have emerged as essential resources. Numerous services are available on these platforms, such as increasing the number of followers, likes, and comments on different social media sites including Facebook, Instagram, Twitter, and YouTube. |
|
|
|
|
|
|
|
|
Consider to transform your social media approach. Increase your online reputation by gaining real followers, real likes, and intelligent comments. Find out SmmPanelPro's capabilities now. |
|
|
|
|
|
|
|
|
The steps provided were clear and easy to follow. I appreciated how the tutorial walked through each step methodically, making it simple to understand how to construct the query and apply the necessary conditions. https://fnfgo.org |
|
|
|
|