Selecting non-matching fields in mySQL - mysql

I have three tables as follows:
Contact, Custom_Field, Custom_Field_Value.
Each contact can have one Custom_Field_Value record for each Custom_Field. So there is a 1:many relationship between Contact and Custom_Field_Value but it isn't quite that simple.
Everything works fine - except for one edge case where I need to select Contacts that have a particular Custom_Field not set (i.e. no corresponding Custom_Field_Value record exists linking to the Contact and the Custom_Field). This is surprisingly difficult. I can't just use the normal "left join and look for NULL" approach because they may have a different custom field - but not the one I am looking for. I need to say "Where Custom_Field_ID=10" but I can't because the thing I'm looking for does not exist.
My line of thinking was heading in this direction, but I'm just tying myself in knots now:
Select ID, First_Name, Last_Name, CF_ID From
(
(Select Contact.ID, First_Name, Last_Name, Custom_Field_Value.ID as CFV_ID, Custom_Field_Value.CustomFieldID as CF_ID, TextValue
From Contact Inner Join Custom_Field_Value on Contact.ID = Custom_Field_Value.ContactID
Where Custom_Field_Value.CustomFieldID=23 Order By Contact.ID)
UNION
(Select Contact.ID, First_Name, Last_Name, Custom_Field_Value.ID as CFV_ID, Custom_Field_Value.CustomFieldID as CF_ID, TextValue
From Contact LEFT Join Custom_Field_Value on Contact.ID = Custom_Field_Value.ContactID
Order by Contact.ID)
) as A
Group BY `ID`, CF_ID ASC
I don't want to create blank records for every possibility because there could be millions of records and every time someone adds a custom field, the database would have to insert millions of corresponding blank records.
It would be really great if we could do this:
Select ID From thingy
EXCLUDE
Select * From thingy Where x = true
This is a nasty one, but I know there'll be someone out there who will love it:)

Okay, I think I have a better understanding now. I was trying to pull it off without a subquery, but I'm not sure if I can.
Can you try
Select Contact.ID, First_Name, Last_Name, Custom_Field_Value.ID as CFV_ID, Custom_Field_Value.CustomFieldID as CF_ID, TextValue
From Contact LEFT Join Custom_Field_Value on Contact.ID = Custom_Field_Value.ContactID
WHERE NOT EXISTS(SELECT * FROM Custom_Field_Value cfv2 WHERE cfv2.ContactID = Contact.ID AND cfv2.CustomFieldID=23)
Order by Contact.ID
The NOT EXISTS subquery should only return rows where the contact has no value for that field.

This is the crazy SQL I ended up with - created dynamically by users. Just publishing it in case its any use to anyone. (Any tips on optimisation are very welcome!):
The problem is that not only do I have to select missing dynamic records, I also have to join together Left Join queries into one result.
SELECT * FROM (
(SELECT * FROM Contact
WHERE (...some dynamic stuff...)
)
UNION All
(SELECT Contact.* FROM Contact Inner Join Contact_Campaign_Link ON Contact.ID=Contact_Campaign_Link.Contact_ID
WHERE ((Campaign_ID=31))
)
UNION All
(SELECT * FROM Contact
WHERE (CustomerID=3)
AND (NOT EXISTS
(SELECT * FROM Custom_Field_Value cfv2
WHERE (cfv2.ContactID = Contact.ID)
AND (cfv2.CustomFieldID =27) )) ORDER BY Contact.ID)
) As tbl
GROUP BY tbl.ID HAVING COUNT(*)=3 Order by ID

Related

Creating a SQL view with personal best records

I have the following SQL Database structure:
Users are the registered users. Maps are like circuits or race tracks. When a user is driving a time a new time record will be created including the userId, mapId and the time needed to finish the racetrack.
I wish to create a view where all the users personal bests on all maps are listed.
I tried creating the view like this:
CREATE VIEW map_pb AS
SELECT MID, UID, TID
FROM times
WHERE score IN (SELECT MIN(score) FROM times)
ORDER BY registered
This does not lead to the wished result.
Thank you for your help!
I hope that you have 'times' table created as the above diagram and 'score' column in the table that you use to measure the best record.
(MIN(score) is the best record).
You can simply create a view to have the personal best records using sub-queries like this.
CREATE VIEW map_pb AS
SELECT a.MID, a.UID, a.TID
FROM times a
INNER JOIN (
SELECT TID, UID, MIN(score) score
FROM times
GROUP BY UID
) b ON a.UID = b.UID AND a.score= b.score
-- if you have 'registered' column in the 'times' table to order the result
ORDER BY registered
I hope this may work.
You probably need to use a query that will first return the minimum score for each user on each map. Something like this:
SELECT UID,
MID,
MIN(score) AS best_time
FROM times
GROUP BY UID, MID
Note: I used MIN(score) as this is what is shown in your example query, but perhaps it should be MIN(time) instead?
Then just use the subquery JOINed to your other tables to get the output:
SELECT *
FROM (
SELECT UID,
MID,
MIN(score) AS best_time
FROM times
GROUP BY UID, MID
) a
INNER JOIN users u ON u.UID = a.UID
INNER JOIN maps m ON m.MID = a.MID
Of course, replace SELECT * with the columns you actually want.
Note: code untested but does give an idea as to a solution.
Start with a subquery to determine each user's minimum score on each map
SELECT UID, TID, MIN(time) time
FROM times
GROUP BY UID, TID
Then join that subquery into a main query.
SELECT times.UID, times.TID,
mintimes.time
FROM times
JOIN (
) mintimes ON times.TID = mintimes.TID
AND times.UID = mintimes.UID
AND times.time = mintimes.time
JOIN maps ON times.MID = maps.MID
JOIN users ON times.UID = users.UID
This query pattern uses a GROUP BY function to find the outlying (MIN in this case) value for each combination. It then uses that subquery to find the detail record for each outlying value.

Wrong use of inner join function / group function?

I have the following problem with my query:
I have two tables:
Customer
Subscriber
linked together by customer.id=subscriber.customer_id
in the subscriber table, I have records with id_customer=0 (these are email records, that do not have a full customer account)
Now i want to show how many customers I have per day, and how many subscribers with id_customer, and how many subscribers WITH id_customer=0 (emailonlies i call them)
Somehow, i cannot manage to get those emailonlies.
Perhaps it has something to do with not using the right join type.
When i use left join, i get the right amount of customers, but not the right amount of emailonlies. When I use inner join i get the wrong amount of customers. Am i using the group function correctly? i think it has something to do with that.
THIS IS MY QUERY:
` SELECT DATE(c.date_register),
COUNT(DISTINCT c.id) AS newcustomers,
COUNT(DISTINCT s.customer_id) AS newsubscribedcustomers,
COUNT(DISTINCT s.subscriber_id AND s.customer_id=0) AS emailonlies
FROM customer c
LEFT JOIN subscriber s ON s.customer_id=c.id
GROUP BY DATE(c.date_register)
ORDER BY DATE(c.date_register) DESC
LIMIT 10
;`
I'm not entirely sure, but I think in DISTINCT s.subscriber_id AND s.customer_id=0, it runs the AND before the DISTINCT, so the DISTINCT only ever sees true and false.
Why don't you just take
COUNT(DISTINCT s.subscriber_id) - (COUNT(DISTINCT s.customer_id) - 1)?
(The -1 is there because DISTINCT s.customer_id will count 0.)
Got it, only risk is that i get no email onlies if there are no customers on this day, becuase of the left join. But this one works:
SELECT customers.regdatum,customers.customersqty,subscribers.emailonlies
FROM (
(SELECT DATE(c.date_register) AS regdatum,COUNT(DISTINCT c.id) AS customersqty
FROM customer c
GROUP BY DATE(c.date_register)
) AS customers
LEFT JOIN
(SELECT DATE(s.added) AS voegdatum,COUNT(DISTINCT s.subscriber_id) AS emailonlies
FROM subscriber s
WHERE s.customer_id=0
GROUP BY DATE(s.added)
) AS subscribers
ON customers.regdatum=subscribers.voegdatum
)
ORDER BY customers.regdatum DESC
;

"Join" admins of different tables into a string

The real issue
Involved tables and their columns
accounts [id,name]
rooms [id,name,topic,owner]
room_admins [account_id,room_id]
Q: Get all rooms with their admin- and owner ids.
Where "all" of course has a condition to it (above: WHERE name LIKE ...)
Admins and owners should be returned in one column just called "admins". I tried to concatenate them above into one string.
What I tried
I came up with a solution, but it requires the use of an omnious external variable ":room_id" that changes on each outer SELECT and makes therefore no sense at all.
SELECT id,name,topic,
(SELECT GROUP_CONCAT(admins.account_id) AS owner
FROM
(SELECT account_id
FROM `room_admins`
WHERE room_id=:room_id
UNION
SELECT owner FROM `rooms` WHERE id=:room_id) admins) AS owner
FROM `rooms`
WHERE name LIKE "%htm%" OR topic LIKE "%htm%" LIMIT 20
Well, I haven't given this a deep thought... but I've just came up with this (sample data would have been useful to make tests... so this is just a blind answer).
select id, name, topic, group_concat(owner_admin) from (
select id, name, topic, owner owner_admin from rooms
union
select id, name, topic, account_id from rooms
left join room_admins on id = room_id
) s
where name like "%htm%" or topic like "%htm%"
group by id, name, topic
Basically I'm just generating a derived table with owner and admins mixed in one column. Then performing the grouping on that mixed column.
Most of the times, when you want to select and display dependent data, you want to use a JOIN. In this case, you want to join the rooms with their admins, so basically:
SELECT r.id, r.name, r.topic, a.id
FROM rooms r
LEFT JOIN admins a
ON r.id = a.room_id
WHERE :condition
Since you have one additional admin not in the admins table (the room owner), you have to (self) join a second time:
SELECT r.id, r.name, r.topic, a.id
FROM rooms r
LEFT JOIN admins a
ON r.id = a.room_id
LEFT JOIN rooms o
ON r.id = o.id
WHERE :condition
This doesn't give us any new information, but your question states that you want to return the list of admins in a single field. So, finally, putting it all together:
SELECT r.id, r.name, r.topic, GROUP_CONCAT(a.id)
FROM rooms r
LEFT JOIN
(
SELECT id, room_id FROM admins
UNION SELECT room.owner AS id, rooms.id AS room_id FROM rooms
) a
ON r.id = a.room_id
WHERE :condition
GROUP BY r.id
But to avoid this ugly sub-select-union clause, I'd advise you to put the room owner into your admin table too.

mysql count one value display multiple columns

I couldnt find this, i'm sure its simple.
Table (196 rows)
tech, name, area, manager
------------------------------
Joe,mary,pa,sally
Kim,brad,ga,tim
kelly,Joe,pa,sally
Joe,jose,pa,sally
a tech is assigned to multiple name/area/managers. I want to do a report that shows all the rows of the table with a column showing the number of assignments for the tech.
My desired results
tech, name, area, manager, (count of number of tech assignments)
Joe,mary,pa,sally,2
Kim,brad,ga,tim,1
kelly,Joe,pa,sally,1
Joe,jose,pa,sally,2
I'm guessing you want a subquery on the SELECT clause:
SELECT
name,
area,
manager,
(SELECT COUNT(*) FROM tablename WHERE tech = x.tech) AS assignments
FROM tablename x
And here is a possibly more efficient way to do the same thing:
SELECT
t.name,
t.area,
t.manager,
sub.assignments
FROM tablename t
INNER JOIN (
SELECT tech, COUNT(*) AS assignments
FROM tablename
GROUP BY tech
) sub
ON sub.tech = t.tech
select
a.tech, a.name, a.area, a.manager,
b.cnt
from table a, (
select count(*) cnt, tech
from table
group by tech) b
where a.tech=b.tech;
Is this what you want?
SELECT tech, count(name) AS total_records FROM db_table GROUP BY tech
Not sure this is what you're looking for though.
Oh yeah, looks like you really need to use subquery.
Thanks for the examples, that helped.
SELECT tech, name, area, manager, assignments
FROM table
INNER JOIN (
SELECT tech, COUNT(*) AS assignments
FROM table
GROUP BY tech
) t
USING (tech)
Explanation:
Select all columns from table and join them with table containing tech column and count of rows from table having particular tech. Keyword USING says how those two table merge.

query to join a table with two fields into one list containing items in both fields

Looking at similar questions, I actually want the exact opposite of this:
SQL query for getting data in two fields from one column
I have a table meetings with paired users:
A_user_id | B_user_id
1 2
3 4
There is a user table as well.
Is there a simple mysql query that lists all the user_ids into one long list?
query result
1
2
3
4
I was thinking something like this but it doesn't work:
select *
from user
where user.id in (
(select A_user_id from meeting)
or
(select B_user_id from meeting)
)
Thanks!
UPDATE (UNION solved this, but let's make this a bit more challenging):
I want to get a list of usernames and location names (both are reference tables) so I need to join this union query to them. Here's what I tried:
select u1.fname, l1.name
from meeting m1
join user u1 on m1.A_user_id=u1.id
join locations l1 on m1.location_id=l1.id
union
select u2.fname, l2.name
from meeting m2
join user u2 on m2.A_user_id=u2.id
join locations l2 on m2.location_id=l2.id
order by location_id asc
I'm getting two errors:
1- Not sure what kind of joins I need on these. (without the last 'order by' line) I'm getting a list of only 2 (there should be 4, as there are 2 pairs of people meeting). It seems to be pulling only the first item from each part of the union. I believe this relates to the type of join I'm doing for each, but not sure. So, users are distinct (there is only 1 user in the meeting table and it matches only 1 user in the user table), but locations are not (2 users are meeting at 1 location, and I think when I join on locations it is messing things up).
2- How do I use the "order by" at the end to order by the resulting list of "location_id"s, since now I have two named tables to deal with.
Thanks!
UPDATE 2:
Ok I put the two selects into parenthesis and UNIONed them and now I can order by the location_id... but I still have no idea how to join on the location table. Mysql doesn't like what I tried
(select u1.fname, m1.location_id
from meeting m1
join user u1 on m1.A_user_id=u1.id)
union
(select u2.fname, m2.location_id
from meeting m2
join user u2 on m2.B_user_id=u2.id)
#join locations l on l.id = location_id // this line messes things up *
order by location_id asc
Doesn't there need to be an all encompassing select around this whole thing?
How do I join the locations.id field on the "location_id" field that gets kicked off of the union query? Since the "location_id" field is technically in two different tables?
THe join above throws an error.
UPDATE 3: SOLVED
Here's my final query:
select tb1.fname, l.name
from (
(select u1.fname, m1.location_id
from meeting m1
join user u1 on m1.A_user_id=u1.id)
union
(select u2.fname, m2.location_id
from meeting m2
join user u2 on m2.B_user_id=u2.id)
) tb1
join locations l on l.id = tb1.location_id
order by location_id asc
select A_user_id as id from meetings
union
select B_user_id as id from meetings
in your example code, you could use an 'or', but the 'or' has to join two 'in' statements, if you get what I mean.
select *
from user
where
(
(user.id in (select A_user_id from meeting))
or
(user.id in ((select B_user_id from meeting))
)
And to answer you second update, you want something like
select locations.* from
(
(select A_user_id as id from meeting)
union
(select B_user_id as id from meeting)
) as UIDS
join
locations on locations.id = UIDS.id
select A_user_id as user_id from meetings
union all
select B_user_id as user_idfrom meetings
order by user_id
Notes:
UNION ALL keeps duplicates, UNION doesn't
Any ORDER BY goes at the end of the UNION