Comparing two tables where one column matches and the other doesn't - mysql

Hi I am trying to compare two tables based on two columns. I to see where the usernames match between the tables and the email addresses don't match.
Here is what I tried:
select * from ecy t1, users t2 where t1.username = t2.username and t1.email <> t2.email
When I run this I get all the users in the ecy table even if their emails are equal.
Thanks.

I wouldn't do the Cartesian Product of the two tables and try to filter it with the old Ansi way.
SELECT u.username, u.email
FROM users u
JOIN ecy e on e.email <> u.email AND u.username = e.username
you could also try doing it with a subquery select that is joined
SELECT username, email
FROM users u
JOIN( SELECT username, email FROM ecy) t on t.username = u.username and t.email <> u.email
DEMO
you want to use the JOIN syntax to join tables together and not do a Cartesian Product. the JOIN syntax will also allow you to filter out data as it joins

you can try this, i didn't use <> before in mysql.
select * from ecy t1, users t2 where t1.username = t2.username and not t1.email = t2.email

Related

Combining multiple queries into a single one

I have a simple query:
SELECT t1.tbl,
t1.slug
FROM t1
WHERE tags = '%".$tag."%'
However, I need to augment my results with the data from other tables (t2, t3, t4 and t5).
For example, if t1.tbl = 't2' I need to add from:
SELECT t2.title
FROM t2
WHERE t2.county = '".$county."'
which I could join like this:
LEFT JOIN ON (t1.rid = t2.id)
In each of there tables I'll filter by $county even though the column is named differently.
I've tried something like this:
SELECT t1.tbl,
t1.slug
FROM t1 A
LEFT JOIN (
SELECT title
FROM t2 B
WHERE A.tbl = 't2'
) ON (A.rid = B.id)
WHERE A.tags = '%".$tag."%'
Is there a way to combine all there into a single query?
SELECT A.tbl,
A.slug,
COALESCE(B.title, C.title) AS title
FROM t1 A
LEFT JOIN t2 B
ON A.tbl = 't2' AND A.rid = B.id AND B.county = ?
LEFT JOIN t3 C
ON A.tbl = 't3' AND A.rid = C.id AND C.region = ?
WHERE A.tags LIKE ?
AND COALESCE(B.id, C.id) IS NOT NULL;
The last condition is to return only rows from A that have a match among one of the joined tables.
I think that's enough to see the pattern, so you can add more tables.
I urge you to learn to use query parameters instead of concatenating variables directly into your SQL string. It's easier to write the code and more secure from SQL injection vulnerabilities if you use query parameters.

MySQL - Joining table on itself based on criteria from other table

Here’s the problem I am trying to solve:
Table1 has Product ID’s, dates, and prices for those dates, Table2 has Product attributes.
I want to be able to compare prices for a client for different products on the same date based on a set of attributes. I’m easily able to get a list of products/dates/prices for a ‘simple’ product, as well as an ‘advanced’ product (see below).
I want to be able to join these two tables such that the output looks like:
[CLIENT] [PRODUCT] [DATE] [SIM_PROD] [SIM_PRICE] [ADV_PROD] [ADV_PRICE]
Here is as far as I've made it
SELECT b.NAME AS ‘CLIENT’, a.NAME AS ‘SIMPLE_PRODUCT’, t1.DATE AS ‘DATE’, t1.PIVOT_PRICE AS ‘SIMPLE_PRICE’
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = TRUE;
SELECT b.NAME AS ‘CLIENT’, a.NAME AS ‘ADV_PRODUCT’, t2.DATE AS ‘DATE’, t2.PIVOT_PRICE AS ‘ADV_PRICE’
FROM TABLE1 t2
LEFT JOIN PRODUCT a
ON t2.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = FALSE;
I've been able to build similar tables where I pull in price from TABLE1 labeling it as t1 then pull in price again from TABLE1 and labeling it as t2, but only when using criteria in TABLE1, not criteria in a table that needs to be joined.
Is it possible to 'set' a table (EG simple) then 'set' a second one (EG advanced) and then join them on PARTNER_ID and DATE?
You can join the two subqueries:
SELECT t1.client, t1.date, t1.simple_product, t1.simple_price, t2.adv_product, t2.adv_price
FROM (
SELECT b.NAME AS CLIENT, a.NAME AS SIMPLE_PRODUCT, t1.DATE, t1.PIVOT_PRICE AS SIMPLE_PRICE
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = TRUE
) AS t1
JOIN (
SELECT b.NAME AS CLIENT, a.NAME AS SIMPLE_PRODUCT, t1.DATE, t1.PIVOT_PRICE AS SIMPLE_PRICE
FROM TABLE1 t1
LEFT JOIN PRODUCT a
ON t1.PRODUCT_ID = a_PRODUCT_ID
LEFT JOIN CLIENTS b
ON a.PARTNER_ID = b.PARTNER_ID
WHERE a.CRITERIA = FALSE
) AS t2
ON t1.client = t2.client AND t1.date = t2.date
You'll probably need to select additional criteria and add them to the ON condition. Otherwise this will produce a full cross product between all the products that have the same client and date.
Your desired output has an additional PRODUCT column, but I couldn't see where that comes from.

MySQL: Putting condition in SELECT query based on column value

I have two tables user_profiles and user_friends.
user_profiles has columns id, user_privacy and few other columns (like username, age etc).
user_friends has columns user_id and friends_id. One user_id can have multiple friend_ids
This query simply returns profile of user having id, say, 1997:
select * from user_profiles
where prfls.id=1997;
And this query returns profile of user having id 1997 only when it has got friend having id, say, 2001:
select * from user_profiles prfls
inner join user_friends frnds on (prfls.id=frnds.user_id)
where prfls.id=1997 and frnds.friends_id=2001;
However, I want to write a single query that will check if column user_privacy (in user_profiles) for user 1997 is false then the query shouldn't check for friends_id in user_friends. It should simply return profile of user 1997. But if the user_privacy is true then only it should check for it.
How can this query be written? (Preferably using joins and without using sub-queries)
Use left join for it:
select distinct t1.* from user_profiles t1
left join user_friends t2 on(t1.id = t2.user_id)
where t1.id = 1997 and
(user_privacy='false' or t2.friends_id = 2001)
Try this;)
select t1.*
from user_profiles t1
where t1.id = 1997
and (
t1.user_privacy = 'false'
or exists (select 1 from user_friends t2 where t1.id = t2.user_id and t2.friends_id = 2001)
)
Without subquery, you can try this;)
select distinct t1.*
from user_profiles t1
inner join user_friends t2
on t1.id = 1997 and (t1.user_privacy = 'false' or (t1.id = t2.user_id and t2.friends_id = 2001))
SELECT *
FROM user_profiles P
LEFT JOIN user_friends F ON F.user_id = P.id
AND P.user_privacy = true
AND F.friends_id = 2001
WHERE P.id = 1997
Use LEFT JOIN so even if user_privacy is false, the query will still return the user_profiles
Add condition in the ON clause of LEFT JOIN indicating that it will return the user_friends if user_privacy is true.
Move the filtering of F.friends_id in the ON clause of the LEFT JOIN. (Thanks to #Msf vtp for verifying, cheers!)

mysql select in case of existing or not

i am trying to write SELECT statement which would return some elements + date difference between current time and date_created or date_edited. If field was edited i should grab date_edited from other table which updates after editing element, if it was not i should grab date_created from current one because that element do not have relationship with other edited elements table, and calculate DATEDIFF(NOW(), date_created/date_edited). I have written select which brings me edited elements with correct date, and other select which brings all elements just if they were edited it brings me incorrect date difference. So all in all i need to form single table which brings me elements with calculated date difference.
SELECT
table1.name,
table1.sales_stage,
users.user_name,
DATEDIFF(NOW(), table1_edit.date_edited) as DaysOnStage
FROM table1
join users on table1.assigned_user_id = users.id
join table1_edit on table1.id = table1_edit.parent_id
WHERE table1.sales_stage = table1_edit.after_value_string;
This one brings all edited values with correct date calculated.
SELECT
table1.name,
table1.sales_stage,
users.user_name,
DATEDIFF(NOW(), table1.date_created) as DaysOnStage
FROM table1
join users on table1.assigned_user_id = users.id;
This one brings me all elements but if they were edited ofcourse with bad date difference. Need somehow to combine these selects and form single table.
Thanks in advance!
You can left join the edit table and put a ISNULL() (IFNULL() in mysql) function on the date field:
SELECT
table1.name,
table1.sales_stage,
users.user_name,
DATEDIFF(NOW(), IFNULL(table1_edit.date_edited,table1.date_created) as DaysOnStage
FROM table1
join users on table1.assigned_user_id = users.id
left join table1_edit on table1.id = table1_edit.parent_id
WHERE table1.sales_stage = table1_edit.after_value_string;
Left/Outer joining tables is a must-learn for working in SQL:
http://www.w3schools.com/sql/sql_join_left.asp
or you can use UNION with your queries:
SELECT
table1.name,
table1.sales_stage,
users.user_name,
DATEDIFF(NOW(), table1_edit.date_edited) as DaysOnStage
FROM table1
join users on table1.assigned_user_id = users.id
join table1_edit on table1.id = table1_edit.parent_id
WHERE table1.sales_stage = table1_edit.after_value_string;
This one brings all edited values with correct date calculated.
UNION
SELECT
table1.name,
table1.sales_stage,
users.user_name,
DATEDIFF(NOW(), table1.date_created) as DaysOnStage
FROM table1
join users on table1.assigned_user_id = users.id;
http://www.w3schools.com/sql/sql_union.asp
I think you can do what you want with a left join and then use COALESCE() to choose the right value:
SELECT t1.name, t1.sales_stage, u.user_name,
DATEDIFF(NOW(), COALESCE(te.date_edited, t1.date_created)) as DaysOnStage
FROM table1 t1 join
users u
on t1.assigned_user_id = u.id LEFT JOIN
table1_edit te
on t1.id = te.parent_id
WHERE t1.sales_stage = te.after_value_string;
if should be helpful
SELECT
table1.name,
table1.sales_stage,
users.user_name,
if (table1_edit.parent_id is null, DATEDIFF(NOW(), table1_edit.date_edited), DATEDIFF(NOW(), table1.date_created)) as DaysOnStage
FROM table1
join users on table1.assigned_user_id = users.id
left join table1_edit on table1.id = table1_edit.parent_id
enter code here

mysql inner join on multiple select statements

Trying to do an inner join on two composite tables, having trouble with the syntax. Here's what I have:
SELECT
count(*)
FROM
(
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID t1
INNER JOIN
(
SELECT DISTINCT UserID FROM Bids, Users WHERE Bids.UserID = Users.userID
)
t2 ON t1.userID = t2.userID
)
I'm guessing it has something to do with the parantheses/lack of as/or whatever. I guess what I'm really asking here is how to give my subqueries aliases, but not using as in the FROM part. Is it valid just to have t1 after User.userID and identify that whole table as t1?
I think this is what you want?
SELECT count(*)
FROM Users
INNER JOIN Items ON Users.userID = Items.seller
INNER JOIN Bids ON Users.UserID = Bids.UserID
You want to name the output table which you get from query
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID
as t1
simple way is use
`select * from (SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID)t1`