Sql query involving two tables - c#

I have two tables, tbl_msg...
tbl_user
I want to select all the msgs from tbl_msg where toid=42 along with respective names of the person(instead of fromid) from whom msg has been sent. Result should look something like this..
|fromid(name, not the id)| Msg| toid(name!,which belongs to id 42)|some other column from msgid|
Query:
select tbl_msg.[MsgId]
,tbl_User.FirstName as sentby
,tbl_msg.[ToId]
,tbl_msg.[Msg]
from
tbl_msg
inner join
tbl_User on tbl_msg.FromId = tbl_User.ID
where
tbl_msg.ToId = 42
but this will only give me name of corresponding fromid and not names for both toid and fromid
How can this be done?

You have to join user two times:
select m.[MsgId]
,u1.FirstName as sentby
,u2.FirstName as sentTo
,m.[Msg]
from tbl_msg m
inner join tbl_User u1 on m.FromId = u1.ID
inner join tbl_User u2 on m.ToId = u2.ID
where m.ToId = 42

Try to change ON tbl_msg.FromId=tbl_User.ID to on tbl_msg.ToId=tbl_User.ID instead:
select tbl_msg.[MsgId]
,tbl_User.FirstName as sentby
,tbl_msg.[ToId]
,tbl_msg.[Msg] from tbl_msg inner join tbl_User
on tbl_msg.ToId=tbl_User.ID
where tbl_msg.ToId=42

Hope this query will helps you.
select tbl_msg.[MsgId]
,u1.FirstName as sentby
,u2.FirstName as FromSent
,tbl_msg.[ToId]
,tbl_msg.[Msg] from tbl_msg
inner join tbl_User u1 on tbl_msg.FromId=u1.ID
inner join tbl_User u2 on u1.ToID = u2.ID
where tbl_msg.ToId=42

Related

Retrieving query data from four tables using SQL Server 2008?

I have four tables:
Customer (ID, CustomerName, City)
Product (pid, pname, sprice)
Orders (OrderNum, CustomerID, EmpID, orderDate)
Sales (OrderNum, pid, qty, totalAmmount, payed, credit, CreditEndDate)
I want to retrieve data from above four tables using the following query, but I encountered a problem where I can alias the sales table?
SELECT
Cs.CustomerName, Cs.City, Crs.totalAmount, p.pname, Crs.qty,
crs.totalAmount, crs.payed, Crs.credit, ord.orderDate,
Crs.CreditEndDate
FROM
Customer Cs
INNER JOIN
Orders ord ON Cs.ID = ord.CustomerID
INNER JOIN
Product p ON p.pid = Sales Crs.pid
WHERE
ord.OrderDate BETWEEN '01/01/2014' AND '01/01/2016'
ORDER BY
[CustomerName]
Is there any help? Thanks in advance.
You need to join Sales table
SELECT Cs.CustomerName,
Cs.City,
s.totalAmount,
p.pname,
s.qty,
s.totalAmount,
s.payed,
s.credit,
ord.orderDate,
s.CreditEndDate
FROM Customer Cs
INNER JOIN Orders ord
ON Cs.ID = ord.CustomerID
INNER JOIN Sales s
ON s.OrderNum = ord.OrderNum
INNER JOIN Product p
ON p.pid = s.pid
WHERE ord.OrderDate BETWEEN '01/01/2014' AND '01/01/2016'
ORDER BY [CustomerName]

How to Join multiple tables?

I tried this query for two tables and it worked, but if I want to do it for many tables how it is done?
cmd.CommandText = "SELECT * FROM assignments
inner join Customers on assignments.Customer_ID = customers.Customer_ID";
//assignments and customers are tables
Consider agents,customer,orders to be your tables & you gotta join them.
SELECT
a.ord_num,
b.cust_name,
a.cust_code,
c.agent_code,
b.cust_city
FROM agents c, customer b, orders a
WHERE b.cust_city = c.working_area
AND a.cust_code = b.cust_code
AND a.agent_code = c.agent_code;
Regards!
Here I am giving one example. You can create queries like this one:
select
*
from tblA a
inner join tblB b
on a.id = b.id
inner join tblC
on a.id = c.id
inner join tblD
on a.id = d.id
If you are using SQL management studio, right click and select "Design Query in Editor". This is the easiest way to join your tables (it's visual)
May be this will help you
SELECT * FROM assignments AS a, customers AS c WHERE a.Customer_ID = c.Customer_ID;

Why this error occurs: Subquery returned more than 1 value

My Sql Query
SELECT BOOKING_TIME,
Contact_No,
(FName+LName)AS NAME ,
E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
INNER JOIN BOOKINGS ON ZM.Zone_ID = BOOKINGS.Zone_ID)AS ZONE_NAME,
City,
Addr_1,
Addr_2,
PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
INNER JOIN BOOKINGS ON PROJECTS.PROJECT_ID=BOOKINGS.PROJECT_ID)AS PROJECT_NAME
FROM BOOKINGS
You're getting that error because of your subqueries:
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
INNER JOIN BOOKINGS ON ZM.Zone_ID = BOOKINGS.Zone_ID) AS ZONE_NAME
And:
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
INNER JOIN BOOKINGS ON PROJECTS.PROJECT_ID = BOOKINGS.PROJECT_ID) AS PROJECT_NAME
You're getting multiple records back and trying to store them in a single field.
Here is your query:
SELECT BOOKING_TIME, Contact_No,(FName+LName)AS NAME, E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM INNER JOIN
BOOKINGS
ON ZM.Zone_ID = BOOKINGS.Zone_ID
) AS ZONE_NAME,
City, Addr_1, Addr_2, PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS INNER JOIN
BOOKINGS
ON PROJECTS.PROJECT_ID=BOOKINGS.PROJECT_ID
) AS PROJECT_NAME
FROM BOOKINGS;
Either subquery could be returning more than one row. In a subselect in the select clause, you can only return one value. I think there is an easy fix. You probably want correlated subuqeries, so just remove the BOOKINGS table from each subquery:
SELECT BOOKING_TIME, Contact_No, (FName+LName)AS NAME, E_MAIL,
(SELECT ZM.ZONE_NAME
FROM Zone_Master ZM
WHERE ZM.Zone_ID = BOOKINGS.Zone_ID
) AS ZONE_NAME,
City,Addr_1,Addr_2,PIN,
(SELECT PROJECTS.PROJECT_NAME
FROM PROJECTS
WHERE PROJECTS.PROJECT_ID = BOOKINGS.PROJECT_ID
)AS PROJECT_NAME
FROM BOOKINGS;
These are now "correlated subqueries". In this case, they should each return at most one row.
Another way to express this query is using join syntax:
SELECT BOOKING_TIME, Contact_No, (FName+LName)AS NAME, E_MAIL,
ZM.ZONE_NAME,
City, Addr_1, Addr_2, PIN,
p.PROJECT_NAME
FROM BOOKINGS b LEFT OUTER JOIN
Zone_Master zm
on ZM.Zone_ID = BOOKINGS.Zone_ID LEFT OUTER JOIN
PROJECTS p
on p.PROJECT_ID = b.PROJECT_ID

LinqToSql Queries instead of some SQL query

SELECT * FROM register WHERE user_id LIKE 'a%'
SELECT * FROM register WHERE user_id LIKE '%m'
SELECT * FROM register WHERE user_id LIKE '%andru%'
SELECT R.name,C.country_name,S.state_name
FROM register R JOIN country C ON R.country_id=C.country_id
JOIN state S ON R.state_id=S.state_id
SELECT R.name,C.country_name,S.state_name
FROM register R INNER JOIN country C ON R.country_id=C.country_id
INNER JOIN state S ON R.state_id=S.state_id
Now i need LinqToSql Queries instead of these query
var result = context.Registers.Select(x => x.StartsWith(foo)).ToList();
result = context.Registers.Select(x => x.EndsWith(foo)).ToList();
result = context.Registers.Select(x => x.Contains(foo)).ToList();
result = from register in context.Registers
join state in context.States on register.state_id equals state.state_id
select new { register.name, state.country_name, state.state_name }
Note, inner join and join function the same in SQL — thus no need to complicate.

How to use the results of the previous query in the next query?

As a result of this query I have a table:
select i.id, o.[name] from Item i
LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20))
where o.name is not null
Now I need to use the result of this table in the next query:
select PriceListItem.ProductExternalId, #id.Id, #id.FriendlyName, #id.BriefWiki,
[PriceListItem].[ProductExternalDesc]
from [#id]
inner join [Product] on Product.ItemId = #name and Product.InstanceId = #id.ID
inner join [PriceListItem] on Product.ID = PriceListItem.ProductId
instead of '#id' I should use data from the table with name= id, and instead of '#name' I should use data from the table with name= name
Standard SQL way, works in most RDBMS
select PriceListItem.ProductExternalId, #id.Id, #id.FriendlyName, #id.BriefWiki,
[PriceListItem].[ProductExternalDesc]
from
(
select i.id, o.[name] from Item i
LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20))
where o.name is not null
)
X
inner JOIN
[#id] ON X.id = #id.id --change here as needed
inner join [Product] on Product.ItemId = #name and Product.InstanceId = #id.ID
inner join [PriceListItem] on Product.ID = PriceListItem.ProductId*
Since you're on SQL 2K8 you can use a CTE:
-- You may need a ; before WITH as in ;WITH
WITH FirstQuery AS (
select i.id, o.[name] from Item i
LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20))
where o.name is not null
)
select PriceListItem.ProductExternalId,
FQ.Id,
-- Neither of these are in your FirstQuery so you can not use them
-- #id.FriendlyName, #id.BriefWiki,
[PriceListItem].[ProductExternalDesc]
from FirstQuery FQ
inner join [Product] on Product.ItemId = FQ.name
and Product.InstanceId = FQ.ID
inner join [PriceListItem] on Product.ID = PriceListItem.ProductId;
From the queries alone it's tough to tell how you plan to JOIN them, but this will allow you to make use of the first query in the subsequent one.
Looks like you have some syntax errors in your second query - #id.id?
select i.id, o.[name] from Item i
into #temp_table
LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20))
where o.name is not null
Now you can use #temp_table as you want :)

Categories

Resources