How to display data from two tables in a SQL Server database in a gridview? - sql

I want show data in a gridview from two tables that have a relationship but it show only some column in table 1. I using an INNER JOIN query.
dept (IDD, deptname)
person (ID, name, birthday, address, IDD)
I want to display the columns ID, name, deptname in a gridview, but deptname is not showing any value.
Thanks!

The select query is straightforward:
select
person.ID,
person.name,
department.deptname
from person
inner join department on person.IDD = department.IDD
If you aren't getting any results, then perhaps no records meet your join criteria. You can change the inner join to a full outer join and examine the results to check if this is the case.

Related

how to do a left join with no duplicate columns?

I have to join 2 tables and the first table I'm joining consists of:
Physicians (ID, FirstName, LastName, PracticeID, SpecialtyID, Email)
and the second table I have is:
PhysicianSpecialties( SpecialtyID, SpecialtyName)
I wrote this query to join the tables together
Select *
from physicians
right join PhysicianSpecialities
on PhysicianSpecialities.SpecialtyID = Physicians.SpecialtyID
and when I left Join them the table is now
(ID, FirstName, LastName, PracticeID, SpecialtyID, Email, SpecialtyID, SpecialtyName)
how can I rewrite this so there is only one "SpecialtyID" Column?
You didn't specify the DBMS product you are using, but: in standard SQL you can join with the USING operator if the join columns have the same name in both tables.
In this case, the "duplicated" column will automatically be removed from the result.
Select *
from physicians
right join PhysicianSpecialities using (SpecialtyID)
Not all DBMS products support that though.
You need to specify the column names of both tables instead of (*)
Select a.*,b.SpecialtyName
from physicians a
right join PhysicianSpecialities b
on b.SpecialtyID = a.SpecialtyID
Use
SELECT
physicians.ID, physicians.FirstName, physicians.LastName,
physicians.PracticeID, physicians.SpecialtyID, physicians.Email,
PhysicianSpecialities.SpecialtyName
instead of SELECT *, hence your query goes like:
SELECT
physicians.ID, physicians.FirstName, physicians.LastName,
physicians.PracticeID, physicians.SpecialtyID, physicians.Email,
PhysicianSpecialities.SpecialtyName
FROM
physicians
LEFT JOIN
PhysicianSpecialities ON Physicians.SpecialtyID = PhysicianSpecialities.SpecialtyID;
I hope it will return the desired result.

Join Query for 3 tables

I need to run a query which joins 3 tables and displays the unique set. I've 3 tables with the row below details:
Users : FName, LName, MobileNumber, STBNumber, CustID.
UserPay : STBNumber, CustID, PreviousDue, CurrentDue, ExpiryMonth,
ExpiryYear.
Calendar: ExpiryMonth, ExpiryMonthName.
Now, the problem with the query is that it is displaying the columns STBNumber, CustID, Month and year twice.
SELECT *
FROM users
INNER JOIN userpay
ON users.stbnumber = userpay.stbnumber
INNER JOIN calendar
ON userpay.expirymonth = calendar.month;
The query should display all the unique columns across all the tables.
So, I was working on a wrong join. Below is the query that solved my problem:
Select Users.FName, Users.LName, Users.MobileNumber, Users.STBNumber, Users.CustID, UserPay.PreviousDue, UserPay.CurrentDue, Calendar.ExpiryMonthName, UserPay.ExpiryYear from UserPay
INNER JOIN Users ON Users.STBNumber=UserPay.STBNumber
INNER JOIN Calendar ON UserPay.ExpiryMonth=Calendar.ExpiryMonth

How to retrieve data from multiple tables using Subquery?

Suppose we have two tables
student(studentID, name, department_ID)
department(departmentID, name).
Our aim is to retrieve the data from both tables using the subquery. I'm trying this
select * from department, student
where department.departmentID
IN (select student.departmentID from student, department
where student.departmentID = department.departmentID)
but it returns the cross product of the number of rows of two tables.
This is possible to get the correct result using JOIN like this
select * from department
Inner join student
on student.departmentID = department.departmentID
and using WHERE clause like this
select * from department, student
where department.departmentID = student.departmentID
I'm wondering if someone can tell me how it can be possible using subquery in SQL.
Hope this helps:
select *, (select name from department d where s.departmentID = d.departmentID) as dname
from student s
where (select name from department d where s.departmentID = d.departmentID) is not null
This problem is however meant to be solved using joins. To learn subQ, use proper examples.
SQL Fiddle: Test

sql server - how to modify values in a query statement?

I have a statement like this:
select lastname,firstname,email,floorid
from employee
where locationid=1
and (statusid=1 or statusid=3)
order by floorid,lastname,firstname,email
The problem is the column floorid. The result of this query is showing the id of the floors.
There is this table called floor (has like 30 rows), which has columns id and floornumber. The floorid (in above statement) values match the id of the table floor.
I want the above query to switch the floorid values into the associated values of the floornumber column in the floor table.
Can anyone show me how to do this please?
I am using Microsoft sql server 2008 r2.
I am new to sql and I need a clear and understandable method if possible.
select lastname,
firstname,
email,
floor.floornumber
from employee
inner join floor on floor.id = employee.floorid
where locationid = 1
and (statusid = 1 or statusid = 3)
order by floorid, lastname, firstname, email
You have to do a simple join where you check, if the floorid matches the id of your floor table. Then you use the floornumber of the table floor.
select a.lastname,a.firstname,a.email,b.floornumber
from employee a
join floor b on a.floorid = b.id
where a.locationid=1 and (a.statusid=1 or a.statusid=3)
order by a.floorid,a.lastname,a.firstname,a.email
You need to use a join.
This will join the two tables on a certain field.
This way you can SELECTcolumns from more than one table at the time.
When you join two tables you have to specify on which column you want to join them.
In your example, you'd have to do this:
from employee join floor on employee.floorid = floor.id
Since you are new to SQL you must know a few things. With the other enaswers you have on this question, people use aliases instead of repeating the table name.
from employee a join floor b
means that from now on the table employee will be known as a and the table floor as b. This is really usefull when you have a lot of joins to do.
Now let's say both table have a column name. In your select you have to say from which table you want to pick the column name. If you only write this
SELECT name from Employee a join floor b on a.id = b.id
the compiler won't understand from which table you want to get the column name. You would have to specify it like this :
SELECT Employee.name from Employee a join floor b on a.id = b.id or if you prefer with aliases :
SELECT a.name from Employee a join floor b on a.id = b.id
Finally there are many type of joins.
Inner join ( what you are using because simply typing Join will refer to an inner join.
Left outer join
Right outer join
Self join
...
To should refer to this article about joins to know how to use them correctly.
Hope this helps.

Three tables join given me the all combination of records

When i written the query like the following.. It's written the combination of all the records.
What's the mistake in the query?
SELECT ven.vendor_code, add.address1
FROM vendor ven INNER JOIN employee emp
ON ven.emp_fk = emp.id
INNER JOIN address add
ON add.emp_name = emp.emp_name;
Using inner join, you've to put all the links (relations) between two tables in the ON clause.
Assuming the relations are good, you may test the following queries to see if they really make the combination of all records:
SELECT count(*)
from vendor ven
inner join employee emp on ven.emp_fk = emp.id
inner join address add on add.emp_name = emp.emp_name;
SELECT count(*)
add.address1
from vendor ven, employee emp, address add
If both queries return the same result (which I doubt), you really have what you say.
If not, as I assume, maybe you are missing a relation or a restriction to filter the number of results.