DLOOKUP vs Multiple SELECT queries - mysql

I need help writing a query that will look up contact names multiple times.
I have two database tables...tblCONTACTS and tblPOLICIES.
tblCONTACTS has two fields, ID and CONTACTNAME. tblPOLICIES has 4 fields POL_ID, OWNER, ASSIGNEE, BENEFICIARY. Other than POL_ID, the other 3 fields are references to ID in the table tblCONTACTS.
How do I write a query to select all of the records in tblPOLICIES, but return the names from tblCONTACTS (instead of the ID that is contained in that field)?
Sorry if this is too amateur for this forum? I think I can do this with a Dlookup function in MS Access but am worried about performance. My other solution was to write multiple queries and the string the multiple queries together.
Thanks in advance.

SELECT
a.POL_ID,
b.CONTACTNAME as OWNER,
c.CONTACTNAME as ASSIGNEE,
d.CONTACTNAME as BENEFICIARY
FROM Test.tblPOLICIES as a
LEFT JOIN Test.tblCONTACTS as b ON a.OWNER = b.ID
LEFT JOIN Test.tblCONTACTS as c ON a.ASSIGNEE = c.ID
LEFT JOIN Test.tblCONTACTS as d ON a.BENEFICIARY = d.ID;
Will give you this result:
Regards

Related

Master Product list from multiple tables with the exact same column names

I’ve got quite a few tables with product information. The columns on each table that I’m pulling from in this particular query have the exact same column names. I’ve been attempting to do it via a UNION ALL but for some reason it is throwing an error saying non-object but all the column names are correct.
I’m using a format that I found online. But obviously something is wrong. There are more tables; however, this is how it starts (with 2). I’d prefer not to have to code each select statement in the union with unique table abbreviations if I don’t have to.
I don’t have to use union if there is a better method.
All tables share data on Product_Categories and Product_Sub_Category.
The only thing unique to each table is id and part_number.
SELECT f.id,f.part_number,f.cat,f.subcat,f.table_name FROM
(
SELECT t.id,t.part_number,psc.name as subcat,c.name as cat, c.table_name FROM Steel_Strapping as t JOIN Product_Sub_Category as psc ON t.subcat = psc.id JOIN Product_Categories as c ON psc.category = c.id ORDER BY c.sort_order,psc.sort_order,t.sort_order
UNION ALL
SELECT t.id,t.part_number,psc.name as subcat,c.name as cat, c.table_name FROM Product as t JOIN Product_Sub_Category as psc ON t.subcat = psc.id JOIN Product_Categories as c ON psc.category = c.id ORDER BY c.sort_order,psc.sort_order,t.sort_order
) f
My end result is one full list of all products sharing column names. Ex: $result[‘part_number’] will pull part numbers from ALL tables listed in union.
I found the solution when playing around with code. I had to add parenthesis (select...) UNION JOIN (select...) inside the parent select statement

Selecting data from joined tables based on one distinct field but returning all other columns

I have looked through many previous questions but can't seem to find one that matches my query?
My current site has customer details stored across different tables within the database.
I have managed to join the tables based on Firstname and Lastname with:
Select *
FROM ps_customer
INNER JOIN ps_address
ON ps_address.Firstname = ps_customer.Firstname AND
ps_address.Lastname = ps_customer.Lastname
However, the results contain duplicated email addresses for the same customers. What I wanted was to do was return all columns from the joined tables but only for distinct/unique email address? is there a command that can do this?
ps I'm very new to sql, so apologies for nontechnical language
I wanted to do something like in this picture enter image description here
Use DISTINCT:
SELECT DISTINCT
pc.ID,
pc.Firstname,
pc.Lastname,
pa.Address1,
pa.Address2,
pa.Zip,
pc.email
FROM ps_customer pc
INNER JOIN ps_address pa
ON pa.Firstname = pc.Firstname AND
pa.Lastname = pc.Lastname;
I don't know exactly which columns you want to select, but the above pattern should be one way to go here.

How to use one table multiple times for inner join in SQL Server?

I am trying to make a table that includes join between 3 tables in the MSSS 2008. There is a fact table, a date table, and a course table. I should join them to make a base table. In date table there is a one parameter that name is Academic Year lookup, and the values in this parameter is like 2000/1, 2001/2. This parameter in the base table should separate to three parameter such as CensusYear, StartYear, and ApplicationYear. Therefore, I need the data table multiple times. I executed a inner join query, and already I have four inner join statement, but I am getting some extra years, and I'm losing some years. I believe, my query should be wrong somewhere.
The attached file is include the design view that created in the MS Access, it'll help to see the tables, and understand what I need to create.
[Design View in Ms Access][1]
SELECT
A.[EventCount],
B.[AcademicYearLookup] AS [CensusYear],
C.[AcademicYearLookup] AS [StartYear],
D.[AcademicYearLookup] AS [ApplicationYear],
B.[CurrentWeekComparisonFlag],
B.[AcademicWeekOfYear],
case
when A.[ApplicationCensusSK] = 1 then 'Same Year'
when A.[ApplicationCensusSK] = 2 then 'Next Year'
when A.[ApplicationCensusSK] = 5 then 'Last Year'
ELSE 'Other'
END as [CensusYearDescription],
B.[CurrentAcademicYear],
A.[StudentCodeBK],
A.[ApplicationSequenceNoBK],
A.[CourseSK],
A.[CourseGroupSK],
A.[CourseMoaSK],
A.[CboSK],
A.[CourseTaughtAbroadSK],
A.[ApplicationStatusSK],
A.[ApplicationFeeStatusSK],
A.[DecisionResponseSK],
A.[NationalityCountrySK],
A.[DomicileCountrySK],
A.[TargetRegionSK],
A.[InternationalSponsorSK] INTO dbo.[BaseTable3yrs]
FROM Student.FactApplicationSnapshot A
INNER JOIN Conformed.DimDate AS B ON A.[CensusDateSK] = B.[DateSK]
INNER JOIN Conformed.DimDate AS C ON A.[AcademicYearStartDateSK] = C.[DateSK]
INNER JOIN Conformed.DimDate AS D ON A.[ApplicationDateSK] = D.[DateSK]
INNER JOIN Student.DimCourse ON A.CourseSK = Student.DimCourse.CourseSK
WHERE (((B.CurrentAcademicYear) In (0,-1))
AND ((A.ApplicationCensusSK) In (1,2,5))
AND ((Student.DimCourse.DepartmentShortName)= 'TEACH ED'));
/* the query to check that the result it's correct or not, and I check it by academic week of year, and I found that I am lossing some data, and I have some extra data, means maybe join is wrong*/
select * from [BaseTable3yrs]
where [StudentCodeBK]= '26002423'
AND [ApplicationSequenceNoBK] = '0101'
order by [AcademicWeekOfYear]
When doing recursive joins like this, it's easy to get duplicate records. You could try gathering the Conformed data separately into a table variable and then joining to it. This would also make your query more readable.
You might also try a SELECT DISTINCT on your main query.

Select specific fields from two inner joins

Given tables Contracts, Contract_Plans and Contract_Plan_Tags, can I select specific fields from all three within an inner join query?
Currently, I have
SELECT * FROM Contracts
INNER JOIN Contract_Plans
ON Contracts.ContractNum = Contract_Plans.ContractNum
INNER JOIN Contract_Plan_Tags
ON Contracts.ContractNum = Contract_Plan_Tags.ContractNum
WHERE Contract_Plan_Tags.tag_id = 44 OR Contract_Plan_Tags.tag_id = 45
This query returns all the fields, but is there any way to select the specific fields from the join tables?
I know I can do
SELECT ContractNum, ContractName FROM Contracts
...
...
but that only selects fields from Contracts, not the other tables.
You should learn about table aliases. The best table aliases are short abbreviations of the tables. You can then list the fields with the aliases:
SELECT c.ContractNum, c.ContractName, cpt.tag_id, . . .
FROM Contracts c
INNER JOIN Contract_Plans cp
ON c.ContractNum = cp.ContractNum
INNER JOIN Contract_Plan_Tags cpt
ON c.ContractNum = cpt.ContractNum
WHERE cpt.tag_id in (44, 45)
I notice that you use table names in the where clause, so I hope I understand the question correctly. You can use aliases (or table names) in the select and where (and group by and having) clauses.
You must prefix the fields by their table name if order for MySQL to distinguish them:
SELECT Contracts.This, Contract_Plan_Tags.That, Contract_Plans.There FROM Contracts
INNER JOIN Contract_Plans
ON Contracts.ContractNum = Contract_Plans.ContractNum
INNER JOIN Contract_Plan_Tags
ON Contracts.ContractNum = Contract_Plan_Tags.ContractNum
WHERE Contract_Plan_Tags.tag_id = 44 OR Contract_Plan_Tags.tag_id = 45
Yes, you can select any field from any table from the FROM clause. If two fields of two tables have the same name, then you must prefix the field with the name of the table (or else you get an error from the parser: "field name is ambiguous").
You may prefix the unambiguous field names too if you find it more readable.
In fact it works exactly the same way as in the WHERE clause.
Incidentally, the same requirement exists if you join tables from several databases. If two tables have the same name, you must prefix their name with the database name, but do not need to if the name is unambiguous.
Oh and you can also do this: SELECT table1.*, table2.some_field, table3.* FROM...

Ok dumb question time. Linq to SQL. Join

I have two tables:
Clients, and a join table that has user id, a foreign key to clients, and some other stuff.
I want to do this SQL
select TblClient.* from TblClient
inner join tblUserClientProjJoin as b on TblClient.Client_ID = b.Client_FK
where b.User_fk = 2
So getting a list of 'clients' that a specific user has access to.
I want to apply this result to collection I can bind to controls.
Anyone?
var q = From c in db.TblClient join
ucp in db.tblUserClientProjJoin on
c.Client_ID equals ucp.Client_FK
select c;
I haven't tested this so you may need to modify it a little.
Hope this helps!