Вопрос

I have two MySQL table HouseHold and UserProfile. Both tables contain the columns HouseHoldID. I want to select all rows from HouseHold Table where HouseHoldID in both tables are equal. How cani do that? I am using the following code

select * from Household where Household.HouseholdID = UserProfile.HouseholdID

But its not giving anything rather showing error. Can anyone help me?

Это было полезно?

Решение 2

You can use a JOIN:

select * from Household
JOIN UserProfile
ON UserProfile.HouseholdID=Household.HouseholdID

Or with an EXISTS:

select * from Household
WHERE EXISTS
(
   SELECT NULL
   FROM UserProfile
   WHERE UserProfile.HouseholdID=Household.HouseholdID
)

Or with an IN:

select * from Household
WHERE Household.HouseholdID IN 
(
   SELECT UserProfile.HouseholdID
   FROM UserProfile
)

Or the old way with the from statement

select * from Household, UserProfile
WHERE UserProfile.HouseholdID=Household.HouseholdID

It kinda depends on what data you want

Другие советы

You have to select from two table
Like the following way:

select * from Household, UserProfile where Household.HouseholdID = UserProfile.HouseholdID

Or Use Join

select * from Household h
JOIN UserProfile u
ON u.HouseholdID=h.HouseholdID

You need to select the UserProfile table as well

select * from Household h JOIN UserProfile u ON h.HouseholdID = u.HouseholdID

You need to JOIN the tables. Try this:

select * from 
Household h JOIN
UserProfile u ON u.HouseholdID =h.HouseholdID 

Learn more about joins here.

Well, the SQL language has very neat way to select records from both tables by given criteria, it is called JOIN:

SELECT H.*, U.* FROM Household H 
INNER JOIN UserProfile U ON U.HouseholdID = H.HouseholdID

You can try:

SELECT A.* FROM Household A INNER JOIN UserProfile B ON A.HouseholdID = B.HouseholdID
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top