Trying to join 4 different tables - mysql

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';

Related

Multiple table update design in Access

I have learned more, and was compelled to find the solutions, mind changed.
PS dear experts, your help is appreciated in the time-saving nature of having forums and discussion in the first place, and also is mined for usefulness, not street cred. get helping or get off. The time I wasted reading posts where the answer was "I don't understand what you are trying to do..." and then questioning the OP with animus or incredulity, or suggesting some unrelated answer further confusing issues, is seemingly the problem with the world these days, if you want to help, help
Original Post:
Ok so as per the comments, thanks to ANYONE who volunteers any help with this problem.
I have a table and relationship design problem.
I have a table with a pk auto and I want to have a related table with a related column incrementally numbered that updates every time new data is entered in the form that is bound to it. It needs to update the related rows in the autonumbered table's key.
---the answer was to join the table on the autonumbered field.
The autonumber of the first table (main recordsource) is just an ID. I think I need two Primary Keys as I need to update the related record with that number in the entry form and move to NextRec but update the pk in the main table and move to NewRec , how do I join (see jpg image)?
I want enter results and update that existing record but update the adjoining pk so that a new record is created in the main table.
Tourney
more in depth:
From yours
My desired form
note that the calculation table feeds the upcoming games table, where a query shows the players recent results. I would like to split the upcoming results to show the players' recent game history, the second tab I would like to enter either one result or many results at a time. I hope I am clearer. You can see why I have had a challenge. See my form though. The recordsource is the upcominggames table and the data entry form is for input (ENTER GAME DATA)
-----the answer to this was an update query (a separate form) and then requery the statistics form to show the new matchups that were entered.
As from the comments, this is what I'm thinking of when I read your description:
Since the UpcomingGames will be entered first and exactly one GameResult can be entered per UpcomingGame, this will be a 1:1 relationship. As the name Upcoming says: The Upcoming data has to be entered before the Result can make sense. Unless an UpcomingGame can be cancelled, there will indeed be a Result for the Game, so there is no need to separate the information into 2 tables. I'd say, a user interface could look like this:
As you can see, the T_NUM column is an autovalue. Before entering any data, I initialized that column using a query like this (and deleted that record afterwards):
INSERT INTO Games ( T_NUM )
VALUES (1004);
This way, the numbering started with number 1005.
You won't be able to to avoid gaps in the numbering, as long as the users can remove existing records or cancel the insertion of a new record. If you want at least to avoid the latter, you will need some VBA code in the form.

Query MySQL database for user names with their email addresses

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

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)

Getting null for every email in my sql table

I'm working with another programmer with a very long list of emails. He sent them to me encrypted, and said I simply need to use aes_decrypt(email, 'passwordsalt') to get them.
I used:
SELECT * FROM mydatabase.emails;
to confirm that i could see the emails (encrypted) in a table. I could. The table consists of three columns: id,email,blank.
However, when I used:
SELECT id, AES_DECRYPT(email,'passwordsalt') AS email, blank FROM mydatabase.emails;
In the column for email all I got was null values. Does anyone know if there is something wrong with my code?

Keeping id's unique Client Side and Server Side

i am scrubbing my head now for hours to solve thw following situation:
Several Html Forms on a webpage are identified by an id. Users can create forms on the clients side themselves and fill in data. How can I guarantee that the id of the form the user generates is unique and that there doesnt occure any collision in the saving process because the same id was generated by the client of someone else.
The problems/questions:
A random function on the client side could return identical id's on two clients
Looking up the SQL table for free id wouldnt solve the problem
Autoincrement a new id would complicate the whole process because DOM id and SQL id differ so we come to the next point:
A "left join" to combine dom_id and user_id to identify the forms in the database looks like a performance killer because i expect these tables will be huge
The question (formed as simple as i can):
Is there a way that the client can create/fetch a unique id which will be later used as the primary key for a database entry without any collisions? Whats the best practice?
My current solution (bad):
No unique id's at all to identify the forms. Always a combination through a left join to identify the forms generated by the specific user. But what happens if the user says: Delete my account (and my user_id) but leave the data on the server. I would loose the user id and this query qouldn't work anymore...
I am really sorry that i couldn't explain it in another way. But i hope someone understood what i am faced with and could give me at least a hint
THANK YOU VERY MUCH!
GUIDs (Globally Unique IDentifiers) might help. See http://en.wikipedia.org/wiki/GUID
For each form the client could generate a new GUID. Theoretically it should be unique.
I just don't show IDs to the user until they've submitted something, at which point they get to see the generated auto-increment id. It keeps things simple. If you however really need it, you could use a sequence table, but it has some caveats which make me advise against it:
CREATE TABLE sequence (id integer default 0, sequencename varchar(32));
Incrementing:
UPDATE sequence
SET id = #generated := id + 1
WHERE sequencename = 'yoursequencename';
Getting:
SELECT #generated;