Question:
how to write Sql query with joints?
ali asgher q
2012-01-05 03:21:57 UTC
Hello,
I have 4 sql table and in each table i want to retrieve 2 columns so any one assist me how to write a query in order to get 4 table information.
when i tried so it omitted with 21 lakes record but in my table it has only 8000 records.
so please assist me,
any suggestion heartily acceptable.
Thanx
Three answers:
Silent
2012-01-05 04:40:59 UTC
We can't really tell you what's wrong with your query if you don't show it to us.



Be aware that this is an international site, and that very few English-speakers outside India or Pakistan are going to know what "lakhs" means.
anonymous
2012-01-05 05:57:10 UTC
My simple answer is:

The best advice I can give you is start selecting data from one table (your primary one), and then slowly join one table at a time. Check your results and row count after each join. This will show you which join is incomplete and resulting in the huge number of records returned.



The detailed answer is:

Without your table structure, this is impossible, however I can give some general guidance (under some major assumptions).

You are using a relational database, so Table1 must have some relationship to Table2 and so on. You need to know the common columns between these tables. If the relationship is 1 to 1 and you are looking for identical values, then you use equals, "=". For example if Table1 has a column called Surname and Table 2 also has Surname, then your code would be:

SELECT *

FROM Table1

INNER JOIN Table2

ON Table1.Surname = Table2.Surname



Of course, no database would be set up using Surname as a key column. More likely it would be some uniquely generated ID (again, I do not have your table structure). So in reality there might be a Person table to store names, with a unique column called Person_ID. Another table might be used to store their addresses called PersonAddress with the relationship between the two tables being Person_ID. In this case the above sample query becomes:

SELECT *

FROM Person

INNER JOIN PersonAddress

ON Person.Person_ID = PersonAddress.Person_ID



The above query would give us all the PersonAddress records for all of the Person records. So, if your Person table has 8000 records and each Person has 1 PersonAddress records, you will end up with 8000 records total. However, if each Person has 2 PersonAddress records (say one address for Home and another address for Work), you will end up with 16000 rows coming back from the query. In reality you will not end up with exactly 1 or two records per Person and the number of rows returned will be random. In reality, additional filtering on the PersonAddress table will be required to only get one address. In my example here, if we wanted the Home address records we would need to add some criteria to get only those records. So, if my PersonAddress table has a column called AddressType and it has the values of "Home" or "Work" and I want only the Home address records, my initial query now becomes:

SELECT *

FROM Person

INNER JOIN PersonAddress

ON Person.Person_ID = PersonAddress.Person_ID

AND PersonAddress.AddressType = "Home" --we only want their home address



You state you are using FOUR tables, so you have to make sure you are only joining from one table to the next on tables that have a common column, if not, you end up with an HUGE number of erroneous records.
Ozzie
2012-01-05 14:50:38 UTC
First you can't with "joints", I hear people smoke those. But, you can with joins.



select a.column1, a.column2,b.column1, b.column2,c.column1, c.column2,d.column1, d.column2

from table_a a

inner join table_b b on a.key = b.key

inner join table_c c on a.key = c.key

inner join table_d d on a.key = d.key





-----



Without knowing the tables and fields it would be something like that. You just need to make a join between each table with one of your key fields. It's preferable if one table can be join to all of the other ones but you can also chain them if two tables have nothing in column but another table does.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...