EF4.1 and Left Outer Join to find nulls? - entity-framework-4.1

I have two tables, Address & People. People has FK to Address. I'm trying to find addresses where there are no people:
select id from Address a
left outer join person p on p.address_id = a.id
where p.address_id is null
Is this possible using LINQ to Entities? I tried a few variations around
var results = from addr in _context.Addresses
from ppl in addr.People
where ppl == null ...
but can't seem to figure out how to return addresses where there are no people.

I propose:
var results = (from addr in _context.Addresses
where !addr.People.Any()
select addr).ToList();

Related

join sql query based on value of another table (in one query)

Let's say that I have two tables A and B where
A is table countries with columns id, name, created, modified
that contains a bunch of countries
And B is table users with columns id, first_name, last_name, email, country_id, created, modified
that contains a bunch of users linked to countries via foreign key country_id
What is the most efficient query to get all the countries that don't have a user with email address "myemail#test.com" associated to it?
I tried something like the following but that didn't work:
SELECT DISTINCT
c.*
FROM
countries c
LEFT JOIN
users u ON u.country_id = c.id
WHERE
u.email <> 'myemail#test.com'
Thanks for any help
NOTE I also tried putting the condition on the email column in the ON clause that didn't work either
A left join is fine, you just need to set it up correctly:
SELECT c.*
FROM countries c LEFT JOIN
users u
ON u.country_id = c.id AND u.email = 'myemail#test.com'
WHERE u.country_id IS NULL;
In terms of performance, this should be pretty similar to NOT EXISTS and NOT IN (although I do not recommend the latter because it has different behavior when there are NULL values).
When you say "that don't have a user with email address "myemail#test.com"",
do you mean no email address -or- not that exact email address?
Updated
Then this should do:
SELECT DISTINCT c.*
FROM countries c
LEFT JOIN users u ON u.country_id = c.id and u.email = 'myemail#test.com'
WHERE u.country_id is null
Which I believe is what Gordon already had.
Updated Again
In that case, try:
SELECT DISTINCT c.*
FROM countries c
INNER JOIN users u ON u.country_id = c.id and ISNULL(u.email, '') = ''
This looks for Null or Empty String email adresses all others are excluded from the join and therefore from the result set.
I hope this helps.

Confused about using UNION or JOIN

I have three MySQL tables: 'people', 'people2id', 'places'. The first one stores information about people, where the 'places' table stores information about their addresses. The middle one, 'people2id', interconnects this two tables (could be that one person has two or more addresses).
Now I want to go to some person's profile and see his profile, but also his associated addresses. I created this query for that:
SELECT * FROM people p
JOIN people2id e ON p.peopleId = e.peopleId
JOIN places a ON a.peopleId = e.peopleId
WHERE p.peopleId = number
This works when the person has associated address(es), otherwise, it will fail. I do not understand if I should use any kind of JOIN or use UNION for this matter.
Change the JOIN to LEFT JOIN, i.e.
SELECT * FROM people p
LEFT JOIN people2id e ON p.peopleId = e.peopleId
LEFT JOIN places a ON a.peopleId = e.peopleId
WHERE p.peopleId = number
Using LEFT JOIN will include all the records from people, whether they contain associated records or not.
UNION is used for get same kind of informations from two different queries and return them as a single result set
JOIN is used for add columns and data to rows (not as simple but you can see it like this)
You should use LEFT JOIN.
SELECT * FROM people p
JOIN people2id e ON p.peopleId = e.peopleId
LEFT JOIN places a ON a.peopleId = e.peopleId
WHERE p.peopleId = number

MySQL LEFT JOIN multiple tables from same source table

I have the following tables:
companyContacts
addresses
states
phoneNumbers
And I need to do a LEFT JOIN on addresses, states, and phoneNumbers. I need to connect phoneNumbers and addresses with companyContacts, and need to connect states with addresses.
I can do a select like the following to grab everything. However, If someone has an address and not a phone number, it will not return a result. How do I make it so that both phoneNumbers and addresses are joined from companyContacts?
SELECT * FROM
companyContacts AS c
LEFT JOIN phoneNumbers AS p
ON c.entityID = p.entityID
LEFT JOIN addresses AS a
ON p.entityID = a.entityID
LEFT JOIN states AS s
ON a.stateID = s.id
It sounds like you want this:
SELECT *
FROM companyContacts AS c
LEFT JOIN phoneNumbers AS p
ON c.entityID = p.entityID
LEFT JOIN addresses AS a
ON c.entityID = a.entityID
LEFT JOIN states AS s
ON a.stateID = s.id
If you join on the companyContacts.entityId to both the phoneNumbers and addresses then you will return records even if there is not a phone number or address. The way you had it written the phone number needed to exist to return an address

mysql left join with multiple where columns

I have a customer table and an address table. Each customer can have multiple entries in the address table, but only one of those entries can be marked as 'primary'. I've put together the query to pull up the customer and their primary address as follows:
SELECT * FROM customers LEFT JOIN addresses
ON customers.cust_id = addresses.cust_id
WHERE customers.status = 1 AND addresses.primary = 1
I've found a flaw in that if the customer has not yet added an address to their account, they will not appear because there is no 'primary' address.
What's the best way to work around this?
SELECT *
FROM customers
LEFT JOIN addresses
ON customers.cust_id = addresses.cust_id
AND 1 = addresses.primary
WHERE customers.status = 1
Try modifying the query to AND (addresses.primary = 1 OR addresses.primary IS NULL)
Simply include the left side data when there is no address.
In the example below I use the primary field, but any field of the addesses table
is null in a left join clause when there is no matching key.
SELECT * FROM customers LEFT JOIN addresses
ON customers.cust_id = addresses.cust_id
WHERE customers.status = 1 AND
(addresses.primary = 1 OR addresses.primary IS Null)

MySQL join of three tables

I have some tables that have 'has and belongs to many' type relationships using join tables. The main tables are countries, programs and listings. The basic structure is:
countries
-id
-name
programs
-id
-name
listings
-id
-title
countries_programs
-country_id
-program_id
listings_programs
-listing_id
-program_id
listings_countries
-listing_id
-country_id
I have been doing the following to list the programs for a country when given the country id value (in this case 5):
SELECT programs.*
FROM programs
LEFT JOIN countries_programs ON programs_id = countries_programs.program_id
LEFT JOIN countries ON countries.id = countries_programs.country_id
WHERE countries_id = '5'
What I need to do is only return programs for the country only if the programs for the specific country actually have any listings that are also in that country. So it should return only the case where the programs are in the country specified and have listings that are in that program and also in that country.
If a program for the specified country has no listings, then I don't want it returned. I've been trying various combinations, but can't seem to get this to work. Does anyone see how this could be done?
I think I need to join the listings table, but nothing I've tried has come close.
To avoid returning duplicate data, I'd use an EXISTS clause. Also, switch to INNER joins to satisfy the country requirement.
SELECT p.*
FROM programs p
INNER JOIN countries_programs cp
ON p.id = cp.program_id
WHERE cp.country_id = 5
AND EXISTS (
SELECT 1 FROM listings_countries lc
INNER JOIN listings_programs lp
ON lc.listing_id = lp.listing_id
WHERE lc.country_id = c.id
AND lp.program_id = p.id
)
I've omitted the join to countries as you're only dealing with the country ID which is available in countries_programs.