Query MySQL database for user names with their email addresses - mysql

I'm looking for a way to query all of my users (simple enough) but i need to get specific users back in the results. I'll try explain the best i can below but in short i want to use a list of email addresses as a reference to pull all users matching those email addresses.
Brief overview
I have a wordpress website which i run events from. When people sign up for the events an account is created for them. there is also another table for attendees which links these user profiles to their registrations.
As it is right now i need to get a list of people specific to one event organiser. I thought the best way to do this would be to use their email address from the attendee info and query my wp_users table for the user names.
SELECT display_name
FROM `mydatabase`.`wp_users` WHERE `user_email` LIKE
'myemail#email.com' OR
'mysecondemail#email.com' OR
'mythirdemail#email.com';
I've removed the real email and changed the database name.
What i am trying to achieve is a query that will run through my list of users comparing their email address that i provide to the users in the table and return any of them that match.
As it is right now it returns the first result it finds and stops.
I've got the list of emails ready to go but i am struggling to get this to work.

You need to repeat the column name like this in the where clause
SELECT display_name
FROM `mydatabase`.`wp_users`
WHERE `user_email` = 'myemail#email.com'
OR `user_email` = 'mysecondemail#email.com'
OR `user_email` = 'mythirdemail#email.com'
or use IN()
SELECT display_name
FROM `mydatabase`.`wp_users`
WHERE `user_email` IN ('myemail#email.com', 'my2email#email.com', 'my3email#email.com')
And like is only used when searching with wildcards. For instance
where email like '%#mydomain.com'

so i managed to figure this out a few minutes after posting this.
I didnt fix the initial issue but i managed to find and create a relationship between the two sets of data in Power BI and get the list i needed.
Thanks though.
PhilB

Related

Trying to join 4 different tables

I realized when trying to query a database I have that I needed to go through four different tables in order to retrieve the data I need. Unfortunately, my brain can not comprehend a JOIN query past two tables so I figured I would ask for help here... I would be very grateful if someone more knowledgeable could help me with that.
Basically, I am trying to figure out the email addresses of the contacts that have been contacted by specific addresses through our Email Marketing Automation Platform that is Mautic.
The way it works is that you have a table with all of the leads with an id and an email address.
You then have a table with every email that has been sent and from which address it has been sent but not to which lead it has been sent. Every email as its own id.
To know to which list of leads an email has been sent, I need to look into a third table that links emails' id to lists' id.
Finally, our fourth table is the list of leads' lists. This is how I can know which lead is part of which list.
So the idea is:
From every email id that has been sent under x#gmail.com, match it to the list of lead its been sent to and check for the leads' id that are inside this lead list. Then retrieve the email of each of these leads' id by looking at the leads table.
My end result should be an email address located in a table called 'ma_leads'
As you can see, each contact has an 'id'. 'id' that we can find in the table `ma_lead_lists_leads' under the column name 'lead_id'.
This table also has a column 'leadlist_id' which we are going to find in the table ma_email_list_xref
This table itself has a column named 'email_id' which we can finally find in ma_emails under the column name 'id'.
This table finally has a "from_address" column that is the filter I talked about initially.
I hope it makes sense. I tried a few JOIN statements but I always seem to make mistakes in the first line so I thought I would turn to this community hoping I can get nudged int he right direction.
Thank you for your time. And I'm obviously going to monitor this thread if any question pops up...
I'd use this :
select l.email
from ma_leads l, ma_lead_lists_leads ll, ma_email_list_xref el, ma_emails l
where l.id = ll.lead_id and ll.leadlist_id = el.leadlist_id and el.email_id = e.id and e.from_address = 'yaniss#sweetspotpr.com';

How to Manage Guest Users with Php and SQL

I am using PHP , MySQL for Handling My site. I have a user table called user_table containing user_id PRIMARY and Now user can chat with other user. For that I am using a table called message_table. this table contains essential fields like msg_id , sender_id , reciver_id , replay_id etc. this works very fine.But I need to handle also guest users[Not registered] with this chat. how can I handle them. I inserted the guset_id as the mixture of entering time and his name to a table called guest_table. Now I can use this guest_table like user_table. Is that a good idea ?
But I need to handle also guest users[Not registered] with this chat.
These guests look to me like a special sort of user. I suggest you handle them as if they were users. You may wish to put a column called something like is_guest in your user table, and set it to 1 when you know the user is a guest.
Keeping a separate table, parallel to the user table, for your guests is NOT a good idea. It will greatly complicate your queries, especially when a person in your user table communicates with a person in your guest table.
Instead, put everybody in the user table, guests and registered users alike.
A nice little benefit: if you convert a guest to a registered user, all you have to do is flip the is_guest flag and let them specify their own username and other data. They get to keep their history.

Access query with join returns checkbox as null value

This is very strange even for my limited knowledge of Access. Background information: I am making a database which track the people I work with. The boss wants a report with the people leaving and the people replacing them, so I though this may work. However when I join the information, it results in the checkbox showing a NULL value. I made sure to set the default to NO and it is not set up for a triple state checkbox from what I can tell.
This is just a sample of what it would look like:
Table
I am using the fields IDReplicate and Replacement ID in order to JOIN the table and query. IDReplicate is just a copy of the Primary Key. Using the primary key as the first field in the JOIN produces a type mismatch
My query to pull information about the replacement people:
SELECT tblExample.IDReplicate, tblExample.Arrival, tblExample.City, tblExample.Package, tblExample.LName
FROM tblExample;
My query to which joins the people leaving and people arriving:
SELECT tblExample.LName, tblExample.Departure, tblExample.ReplacementID, qryReplacement.*
FROM tblExample LEFT JOIN qryReplacement ON tblExample.ReplacementID = qryReplacement.[IDReplicate];
Resultant Query Datasheet
It sounds like you may have a three-state checkbox where you want a two-state, check this SO post for details:
Determine whether a Access checkbox is checked or not
IMO, checkboxes are trouble and best avoided. My advice would be to change the field to a text field with a constraint to limit entry to yes or no, defaulting to no. That way you know exactly what data is really in your table, instead of counting on Access to translate an interface element into data. If you do it in the Access table design interface it would look something like this:

Which will be the better database design in following situation? Why?

First I would like to clear the scenario.
There is bulk mail system have lists(groups of emails) and every lists has multiple contacts(emails). the contacts have some states like normal,unsubscribed,bounced etc.
I am thinking to design something like this.
Table Contacts
-----------------------------------------
id email group_id contacts status
-----------------------------------------
status will store the email ids states which is stated above.
But one list can have a thousands of email and so if the client increases, retrieving of data will be caused to performance issue
So I get the two solutions
To make the indexing for status column
to make a different table for different contact status like
one table for normal added contacts
another for unsubscribed contacts.
and so on for other states
So my question is which will be the better solution for above situation.
Please guide me.
I am going to bind the lists and contacts in many to many relationship above table is just for describing the problem.
Thanks in advance.
Update
Queries for retrieving the information from above table
for retrieving normal contacts
select * from contacts where group_id = '[group_id]' and status = 1
for unsubscribed contacts
select * from contacts where group_id =[group_id]' and status = 2
and so on.
For you comment put an index on group_id and status
i.e.
create index my_index on contacts(group_id, status)
That will solve any performance problem as the database can find the list without locking the table.
Might be an idea to not perform any expensive operations whilst scanning that list (e.g. emailing)

How to change custom fields sql query on redmine

I use zapier to send sms to users when a new issue is created on Redmine.
I want to make it easy for users and sophisticated.
To do so; I've created a custom field on USER section which is named MOBILE. In Custom Fields menu, the user writes his/her phone number in this field.
I also created a custom field on ISSUES section. This custom field brings user list to ISSUES. User list custom field as you know is default redmine feature in ISSUES section for redmine 2.4.x.
So here is my question; I want user list custom field feature to bring user MOBILE numbers which I've created field on users accounts page. Current query brings name and surname and user to.
I checked apps-models-custom_field.rb however no luck. I couldn't find a solution.
I think inner join with custom_values table could solve my problem but I do not know how to do it, actually I don't know which file contains this queries for custom field user list.
Default user list feature of custom fields prints User firstname and lastname on ISSUES page also it just brings user id on the xml api.
So all I want to do is get user custom field content with their user id on the xml.
I hope I've made myself clear.
What are your suggestions?
You won't be able to find SQL queries in Redmine code as Redmine uses Rails' ActiveRecond framework to build SQL queries dynamically.
As I understand the query, you are looking for, should look like:
SELECT
login, firstname, lastname, value
FROM
users
LEFT JOIN
custom_values ON custom_values.customized_id = users.id
AND customized_type = 'Principal'
WHERE
custom_field_id = (SELECT id FROM custom_fields
WHERE type = 'UserCustomField'
AND name = 'MOBILE')
AND value IS NOT NULL
AND value != ''
Hope, this helps.