Mysql query, selecting two values in one field - mysql

In the table above table 'sent' I use, among other things, two fields: 'opened' and emailnumber.
I'm looking for a mysql query that the people who opened email number 1 and/or email number 2 one or more times
So in the example it would be everyone, except Fred
I think it's simple, but I can't find the solution

SELECT name
FROM table
GROUP BY name
HAVING SUM(open > 0);

Related

MySQL subquery with if conditionals

I'm querying a table from a third party plugin so I don't have control over how the data is being inputted. It's a course plugin broken into 4 different quizzes. One of the questions is being used in all four quizzes.
THERE IS NO "quiz_id" which is why I think they only way to query data is with some sort of if conditional. There IS a date field and a unique id field.
This is what my subquery looks like:
(SELECT y.post_content
FROM wp_posts AS y
WHERE 137 = y.post_author
AND y.post_title LIKE '%If you checked "Other" in "What are your organization’s primary goals related to hiring people with autism?", please explain%'
ORDER BY y.post_author ASC limit 1)
This works to query the answer (y.post_content) for the first quiz, but not for all 4 quizzes. Is there a conditional I can use for this? i.e. let's say i'm querying results for the 2nd quiz: if there are four answers, pick the 2nd one,if there are 3 answers pick the 2nd one, if there are 2 answers, pick the most recent one

Finding duplicate records by searching a few characters

I know there are many questions answered under this title. But I believe I have a unique situation where I need a specific way to find and export the duplicate data from my database.
I have a database with over 20.000 contacts. I need a query to find duplicate records in the contacts table. But since there are many same last names, or first names for different people, I want to lookup the first few characters of the first name and the last name to see if there is a duplicate record matching the query.
So, the query could be explained like this: Look at the first two characters from the firstName column, then look at the first three characters from the lastName column, and show it with any similar records.
I would highly appreciate any advice. Thank you.
Here's how I'd do it, assuming your contacts table is called contacts:
select *
from contacts c
join contacts c2 on c2.id!=c.id
and left(c2.firstName,2)=left(c.firstName,2)
and left(c2.lastName,3)=left(c.lastName,3)

MYSQL - Finding value on table when all rows on different table exist

Sorry if the titles abit wrong been stuck on this for a while now and my brain is well and truely frazzled at this point..
I've simplied the question to the basics. There are 2 tables..
TABLE A
------------
-id
TABLE B
-------------
-table_a_fk
-user_id
I'm trying to find the ID of Table A using a list of user_ids... and only if the entry exists in Table B for all users..
So if the user_ids were 10,11,12 the query would only return a result if Table B had rows for users 10,11,12 with the same table_a_fk. It's quite important too that if there is extra entries (i.e users 10,11,12,13,14) with the same table_a_fk in Table B then this does not return.. it needs to be an exact match.
I'm starting to think what I need is impossible just in MYSQL and I'll need to process the rows with a more general search... would love for one of you SQL gurus to prove me wrong though ;)
Many thanks in advance for an help and suggestions
UPDATE: Just realiased that table A might not even be needed in this query... as if the user_ids all match then the value of table_a_fk would be the same as taking it from Table A.
Like I said my brains not working atm aha... thanks for any help!
This is an example of a set-with-set query. I like to handle this using group by and having:
select table_a_fk
from b
group by table_a_fk
having sum( b.user_id in (10, 11, 12) ) = 3 and
count(*) = 3;
You need to adjust the in list and the 3 to match the list of users you care about and the number of such users. This assumes that there are no duplicates in the table.

Find first, second, third, and so forth record per person

I have a 1 to many relationship between people and notes about them. There can be 0 or more notes per person.
I need to bring all the notes together into a single field and since there are not going to be many people with notes and I plan to only bring in the first 3 notes per person I thought I could do this using at most 3 queries to gather all my information.
My problem is in geting the mySQL query together to get the first, second, etc note per person.
I have a query that lets me know how many notes each person has and I have that in my table. I tried something like
SELECT
f_note, f_person_id
FROM
t_person_table,
t_note_table
WHERE
t_person_table.f_number_of_notes > 0
AND t_person_table.f_person_id = t_note_table.f_person_id
GROUP BY
t_person_table.f_person_id
LIMIT 1 OFFSET 0
I had hoped to run this up to 3 times changing the OFFSET to 1 and then 2 but all I get is just one note coming back, not one note per person.
I hope this is clear, if not read on for an example:
I have 3 people in the table. One person (A) has 0 notes, one (B) with 1 and one (C) with 2.
First I would get the first note for person B and C and insert those into my person table note field.
Then I would get the second note for person C and add that to the note field in the person table.
In the end I would have notes for persons B and C where the note field for person C would be a concatination of their 2 notes.
Welcome to SO. The thing you're trying to do, selecting the three most recent items from a table for each person mentioned, is not easy in MySQL. But it is possible. See this question.
Select number of rows for each group where two column values makes one group
and, see my answer to it.
Select number of rows for each group where two column values makes one group
Once you have a query giving you the three rows, you can use GROUP_CONCAT() ... GROUP BY to aggregate the note fields.
You can get one note per person using a nested query like this:
SELECT
f_person_id,
(SELECT f_note
FROM t_note_table
WHERE t_person_table.f_person_id = t_note_table.f_person_id
LIMIT 1) AS note
FROM
t_person_table
WHERE
t_person_table.f_number_of_notes > 0
Note that tables in SQL are basically without a defined inherent order, so you should use some form or ORDER BY in the subquery. Otherwise, your results might be random, and repeated runs asking for different notes might unexpectedly return the same data.
If you only aim for a concatenation of notes in any case, then you can use the GROUP_CONCAT function to combine all notes into a single column.

How to query 3 mysql tables and return matching results (with one to many relationships)?

I am trying to query a database to return some matching records and can't work out how to do it in the most efficient way. I have a TUsers table, a TJobsOffered table and a TJobsRequested table. The UserID is the primary key for the TUsers table and is used within the Job tables in a one to many relationship.
Ultimately I want to run a query that returns a list of all matching users based on a particular UserID (eg a matching user is one that has at least one matching record in both tables, eg if UserA has jobid 999 listed in TJobsOffered and UserB has jobid 999 listed in TJobsRequested then this is a match).
In order to try and get my head around it i've simplified it down a lot and am trying to match the records based on the jobids for the user in question, eg:
SELECT DISTINCT TJobsOffered.FUserID FROM TJobsOffered, TJobsRequested
WHERE TJobsOffered.FUserID=TJobsRequested.FUserID AND
(TJobsRequested.FJobID='12' OR TJobsRequested.FJobID='30') AND
(TJobsOffered.FJobID='86' OR TJobsOffered.FJobID='5')
This seems to work fine and returns the correct results however when I introduce the TUsers table (so I can access user information) it starts returning incorrect results. I can't work out why the following query doesn't return the same results as the one listed above as surely it's still matching the same information just with a different connector (or is the one above effectively many to many and the one below 2 sets of one to many comparisons)?
SELECT DISTINCT TUsers.Fid, TUsers.FName FROM TUsers, TJobsOffered, TJobsRequested
WHERE TUsers.Fid=TJobsRequested.FUserID AND TUsers.Fid=TJobsOffered.FUserID AND
(TJobsRequested.FJobID='12' OR TJobsRequested.FJobID='30') AND
(TJobsOffered.FJobID='86' OR TJobsOffered.FJobID='5')
If anyone could explain where i'm going wrong with the second query and how you should incorporate TUsers then that would be greatly appreciated as I can't get my head around the join. If you are able to give me any pointers as to how I can do this all in one query by just passing the user id in then that would be massively appreciated as well! :)
Thanks so much,
Dave
Try this
SELECT DISTINCT TJobsOffered.FUserID , TUsers.FName
FROM TJobsOffered
INNER JOIN TJobsRequested ON TJobsOffered.FUserID=TJobsRequested.FUserID
LEFT JOIN TUsers ON TUsers.Fid=TJobsOffered.FUserID
WHERE
(TJobsRequested.FJobID (12,30) AND
(TJobsOffered.FJobID IN (86 ,5)
You need to add "AND TJobsOffered.FUserID=TJobsRequested.FUserID" to your where clause.