SQL Server: one simple question - c#

So I have a table Transportation with columns: ClientIDAsSeller, ClientIDAsBuyer..
and a Client table with column ID (Primary key). My C# app gets a client IDs and sets in the Transportation table.
When I press execute result is null
What is problem how can I solve that?
SELECT
Clients.Name, Transportation.TransStart, Transportation.TransEnd
FROM
Transportation
INNER JOIN
Clients ON Transportation.ClientIDAsSeller = Clients.ID
AND Transportation.ClientIDAsBuyer = Clients.ID

You are likely excluding all possible result sets by only doing one join. You probably want to do:
SELECT
S.Name as SellerName
B.Name as BuyerName,
Transportation.TransStart,
Transportation.TransEnd
FROM Transportation
INNER JOIN
Clients S
ON
Transportation.ClientIDAsSeller = S.ID
INNER JOIN
Clients B
ON
Transportation.ClientIDAsBuyer = B.ID

Related

Passing the value of property from form and store in variable to do join in LINQ using that variable

I am taking the input from user and store in variable. then I am selecting the data from database using LINQ to do that I have to perform a join condition and the property of that join should change according to what user enter if user select location then it should be e.Location and if he select department it should be e.department . the join condition both the side is a INT and while storing the data in variable I am getting the value in string, for example user select location so the variable is string variable , so on join it should be changed to INT to perform proper join.
In my model class I have 4 property location, department, designation, Etiolation. from front end user select any of this so it should do store in variable then it fetch value from database I have written the below query. it should join with other table to fetch value so this is a case, if user select any of this it should be join with other table for that I want to use variable to join the table .
before making the value change according to user select .
var param = form["Category"];
var query = (from e in table1 join c in table2 on e.location equals c.Id select e);
doing some thing with the query data
and how I want it to be ,
var param = form["Category"];
var query = (from e in table1 join c in table2 on e.param equals c.Id select e);

.NET SQL Query joining two different tables

I'm brand new to .net MVC, and while I have some basic experience with writing SQL queries, I'm not sure how to go about what I need to do for .NET.
My initial query looks like this:
var results = (from s in db.Members
join sa in db.FocusArea on s.ID equals sa.MemberID
where sa.Area == SearchString
select new { s.ID, s.Name, s.Overview }).ToList();
This is not functioning correctly. It is seaching in the s.Overview for some reason. And, I need to make this query much more complicated. In short, I have three tables I need to search across. And with this query, it is not working:
var conceptResults = (from s in db.Cohorts
join oa in db.OutcomeArea on s.ID equals oa.CohortID
where ((oa.Area.Contains(SearchString))
|| (oa.OutcomeType.Contains(SearchString)))
select new { s.ID, s.Name, s.Overview }).ToList();
I need to use my SearchString to search for matches in both Area and Description in db.FocusArea.
I also need to use my SearchString to search for matches (contains) in another table db.SpecificFocusAreas for column SFocusArea where again the join is the ID/MemberID.
Is there a way to essentially do a join or join type of statement? I don't want to join all three tables because I am looking for results from either of the two joins, not from all joins.

C# SELECT statement, SQL query

I am trying to get all the products from the Products table, and at the same time retrieve Company_Name from Company table. A common column in both my table is the Company_Id.
I am using this query:
SELECT
products.product_id,
products.product_name,
products.product_desc,
products.unit_price,
products.stock_level,
products.product_image,
products.gender,
products.type_of_acct,
products.product_cname,
products.product_cdesc,
products.company_id,
company.company_name
FROM
products
INNER JOIN
company ON products.company_id = company.company_id
However this only show all the products from a specific company.
I need to show all the products.
It seems you have an optional relationship here, so use LEFT JOIN:
....
FROM Products
LEFT JOIN Company
ON Products.Company_Id = Company.Company_Id
This retrieves all the products whether linked to a valid company or not.
I think you also need to go over your data and check if you have your foreign keys set up right and have the correct data.
You can use LEFT JOIN to get all details from Products table. LEFT JOIN is fetch all records from left table and also fetch matching records from right table.
SELECT Products.Product_ID, Products.Product_Name, Products.Product_Desc, Products.Unit_Price, Products.Stock_Level, Products.Product_Image, Products.Gender, Products.Type_Of_Acct, Products.Product_CName, Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM Products
LEFT JOIN Company ON Products.Company_Id = Company.Company_Id
Left join is best solution for you.
Or you can make one user define function and from there you can retrieve company name like below
SELECT
Products.Product_ID, Products.Product_Name,
Products.Product_Desc, Products.Unit_Price,
Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct,
Products.Product_CName, Products.Product_CDesc,
Products.Company_Id,
dbo.GetCompanyNameFromCompanyID(Products.Company_Id) AS Company_Name
FROM
Products
Try this
SELECT
Products.Product_ID, Products.Product_Name, Products.Product_Desc,
Products.Unit_Price, Products.Stock_Level, Products.Product_Image,
Products.Gender, Products.Type_Of_Acct, Products.Product_CName,
Products.Product_CDesc, Products.Company_Id, Company.Company_Name
FROM
Products
LEFT JOIN
Company ON Products.Company_Id = Company.Company_Id
This will return you all the products, with its linked company if any, a NULL will be shown under Company.Company_Name otherwise

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;

left outer join from different database

i'm having trouble with left outer join
i only want the cj_id(table1 in bd1) where c.ref(table1 in bd1) is not found in ref(table2 in bd2)
so i can remove some registries from db1 that have cj_id instead of ref
for this i'm using this code:
var query1 = from a in dbPT.table2
join b in dataB.table1
on a.Ref equals b.ref into c
from x in c.DefaultIfEmpty()
select x.CJ_ID;
i can't get it to return a string with a cj_id
has you can see in here Joining Two Entity Sets from Different Contexts
the join is done in the server side, and if you use diferent server there is no way you can do this

Categories

Resources