MySQL Complicated Contacts Table - mysql

I am trying to output a list of contact names with their phone number and email address for a given company.
The problem I am facing is getting it to output a contact based on the following criteria:
Contacts may or may not have a name, email or telephone, but they must have at least one of these to appear in the results.
There can be more than one contact per company.
There can be more than one email address and/or telephone number per contact.
There is a primary flag on contacts, so if there is more than one contact and one of them is primary, it should pick that one instead of the other non-primary one.
I have tried the following for getting the contacts name but with no success:
SELECT entity_details.name,
COALESCE(
(SELECT entity_contacts.name FROM entity_contacts
WHERE entity_contacts.entityRef = entity_details.id
ORDER BY entity_contacts.isPrimary = 1),
(SELECT entity_contacts.name FROM entity_contacts
WHERE entity_contacts.entityRef = entity_details.id)
)
AS contact
FROM entity_details
WHERE entity_details.ownerRef = ?
This is the closest thing I can get to but I am unsure of if it's correct or not, and it doesn't prioritise primary contacts, it just selects any and groups on the entityRef to remove duplicates:
SELECT
entity_details.name, entity_contacts.name AS contact,
entity_contacts_telephones.tel, entity_contacts_emails.email
FROM entity_details
LEFT JOIN entity_contacts ON entity_details.id = entity_contacts.entityRef
LEFT JOIN entity_contacts_telephones ON entity_contacts.id = entity_contacts_telephones.contactRef
LEFT JOIN entity_contacts_emails ON entity_contacts.id = entity_contacts_emails.contactRef
WHERE entity_details.ownerRef = ?
GROUP BY entity_contacts.entityRef
LIMIT ?, ?
All tables are Innobd, the ones I am working with are in the above edit. All references etc have indexes on where nesisary.
In entity_details there around about 13000 rows, 12000 in entity_contacts, and a few 1000 in entity_contacts_telephones and entity_contacts_emails.
I thought the following would work but it doesn't:
LEFT JOIN entity_contacts_telephones
ON entity_contacts.id = entity_contacts_telephones.contactRef
AND COALESCE(entity_contacts_telephones.isPrimary = 1, 0)

This may work (not sure mainly because it's not clear how many rows per entity you can have in each table):
SELECT
entity_details.name,
( SELECT entity_contacts.name
FROM entity_contacts
WHERE entity_contacts.entityRef = entity_details.id
ORDER BY entity_contacts.isPrimary DESC
LIMIT 1
) AS contact
FROM entity_details
WHERE entity_details.ownerRef = ?
You probably need a join - of the [greatest-n-per-group] type:
SELECT
d.name,
...
c.whatever
...
FROM
entity_details AS d
JOIN
entity_contacts AS c
ON c.PK = --- the PRIMARY KEY of contacts table
( SELECT cc.PK
FROM entity_contacts AS cc
WHERE cc.entityRef = d.id
ORDER BY cc.isPrimary DESC
LIMIT 1
)
WHERE d.ownerRef = ?
An index on (entityRef, isPrimary) would help improving peformance.

Related

How to export user data from two different tables using the backoffice sql manager of Prestashop?

I'm not good with sql so i need help exporting my customer's first names, email address, their country and if possible last times they had access to the store.
I managed to get email and first name by using this query :
SELECT firstname, lastname, email, active FROM psdb_customer
From what i understood, since the other data is stored in a different db table, i should use a join to get data from both tables but i haven't been able to figure out how
Any help is welcome
In the past, I helped someone with something similar in the PrestaShop forum. This query should work, just remember that to get a country of a customer they should have at least one address registered and of course I'm using the default db prefix:
SELECT a.`firstname`,
a.`lastname`,
a.`email`,
(SELECT c.`date_add`
FROM `ps_guest` g
LEFT JOIN `ps_connections` c ON c.`id_guest` = g.`id_guest`
WHERE g.`id_customer` = a.`id_customer`
ORDER BY c.`date_add` DESC LIMIT 1) AS `last_activity`,
(SELECT cl.`name`
FROM `ps_address` ad
LEFT JOIN `ps_country_lang` cl ON cl.`id_country` = ad.`id_country`
WHERE ad.`id_customer` = a.`id_customer`
ORDER BY ad.`id_address` DESC LIMIT 1) AS `country_name`
FROM `ps_customer` a
Rolige's answer is what i was looking for.
Here is another query that allows filtering the results by country (using the id_country)
SELECT SQL_CALC_FOUND_ROWS a.`id_address`,
a.firstname as firstname,
a.lastname as lastname,
cl.id_country as country, cl.`name` as country
FROM `ps_address` a
LEFT JOIN `ps_country_lang` cl ON (cl.`id_country` = a.`id_country`
AND cl.`id_lang` = 1)
LEFT JOIN `ps_customer` c ON a.id_customer = c.id_customer
WHERE 1 AND a.id_customer != 0
AND c.id_shop IN (1)
AND a.`deleted` = 0
AND cl.`id_country` = '8'
ORDER BY a.`id_address` ASC

SQL: count rows from 3rd table

I have 3 tables, the first table is the account_has_account1 where i store the relation between accounts and it's columns are account_id, account_id1, status where account_id is the account doing following to account_id1 and status is an enum type with values active, inactive where active indicates if the account is actually following, if it's inactive, then account stopped following.
the second table is named account_has_photos which i store the photos one account has stored in the database, so it's columns are account_id, photos_id, so i need this table to get all photos from one account which another account is following.
But all these have messages posted on them, and here is where the 3rd table comes which is named photos_has_message_photos, from this table i only need a count of all posted messages in one photo, the columns are photos_id, message_photos_id
for now my query is this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
it shows all photos from accounts on which account id 7 is following, but on my attempts on getting the total messages have failed, i thought on doing an INNER JOIN like this:
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
and then i select from main posts.total, but it does not show any row, not even the photos, the result is empty at this point and i have no idea why and what to do.
the complete query is like this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id, posts.total
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
again, i only need a total of rows which are messages from each photos found
Try this query updated inner select
SELECT ahp.photos_id as id, "photos" as type, ahp.update_at, ahp.account_id,posts.total
FROM account_has_account1
JOIN account_has_photos
ON (ahp.account_id = account_has_account1.account_id1 AND ahp.type_id = 17) INNER JOIN (
SELECT phmp.photos_id, count(*) as total FROM photos_has_message_photos GROUP BY phmp.photos_id
) posts
ON(posts.photos_id = ahp.photos_id) WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"

mysql SELECT EXISTS on multiple tables

Have tables: person,person_ip
Both tables have pid column as a primary key, in table person there is column state_id, in table person_ip there is column ip.
Want to discover if specified IP address is assigned to person with state_id is not equal to 2. But always got result 1, even if state_id is 0, 1 or 2. Always got 0 only if ip address is not listed at all. What am I doing wrong?
SELECT EXISTS (
SELECT person_ip.PID
FROM person_ip,person
WHERE person.PID=person_ip.PID
AND person.state_id NOT IN (2)
AND person_ip.ip='10.11.12.13'
)
this seems like a simple join.. unless i'm missing something
select person.*
from person
inner join person_ip
on person.pid = person_ip.pid
where person.state_id <> 2
and person_ip.ip_address = '10.0.0.1'
If you want to exclude the ip_address if it has been assigned to any user with state = 2, even if it has also been assigned to a user without state = 2, then try:
select max(i)
from (
select *
from (
select 1 as i
from dual
where not exists (
select 1
from person p
inner join person_ip pi
on p.pid = pi.pid
where p.state_id = 2
and pi.ip_address = '10.0.0.1'
)
) q
union
select 0
) qq
(dual is a system table that can be used as a sort of stub table)
here's a fiddle showing both versions
update after some actual sleep
Okay, so the above query is a little.. out there. Back in the real world, this one is probably more appropriate:
select count(case when p1.state_id = 2 then 1 end)
from person p1
inner join person_ip pi1
on p1.pid = pi1.pid
where pi1.ip_address = '10.0.0.1'
group by pi1.ip_address;
This will return 1 or more if your ip_address has been used by someone with a state_id of 2, and 0 if it has never been used by someone with a state_id of 2.
It will return nothing if the ip has never been used.
this fiddle has all three of the above queries.
SELECT IF(COUNT(*)>0,1,0)
FROM person
INNER JOIN person_ip
ON person.pid = person_ip.pid
AND person_ip.ip_address = '10.0.0.1'
WHERE person.state_id <> 2

Performing a SUM over nested Foreign Key relationships

I have some tables roughly like so:
Client:
id
name
Employee
id
name
Email
id
to : Client [ForeignKey]
from : Employee [ForeignKey]
EmailStats (Tracks the stats for a particular single email)
id
email : Email [OneToOne]
curse_words : 10
What I want to do: I want to fetch all the employees that have written at least one email to a single client, along with the number of times they've cursed in any of their emails to that single client, i.e. for a particular Client return
[
('name' : 'Paul', 'total_curses' : 255),
('name' : 'Mary', 'total_curses' : 10),
]
What I've tried:
My understanding of SQL is quite weak as I'm used to using ORM's. I'm having trouble understanding how the normal retrieval of Employees links into the counting of the curse words. Here's what I've done (be kind!):
SELECT DISTINCT (
SELECT SUM(EmailStats.curse_words)
FROM EmailStats
INNER JOIN (
SELECT Email.id
FROM Email
INNER JOIN Employee
ON Email.from = Employee.id
WHERE Email.to = 5 // Assuming 5 is the client's id
) filtered_emails ON EmailStats.email = filtered_emails.id
) AS 'total_curses', Employee.name
FROM Employee
INNER JOIN Email
ON Email.from = Employee.id
WHERE Email.to = 5 // Assuming 5 is the client's id
ORDER_BY 'total_curses'
This isn't working - it seems to fetch the correct Employees (those who have sent to the Client) but the curses count seems to be the total for all emails to that Client instead of just those curses from that Employee.
I've got a feeling that I'm gravely misunderstanding something here, so if anyone could provide an example of how to succesfully go about this I'd appreciate some pointers.
You want to group the result of joining your tables:
SELECT Employee.name, SUM(EmailStats.curse_words) total_curses
FROM Email
JOIN EmailStats ON EmailStats.email = Email.id
JOIN Employee ON Employee.id = Email.from
WHERE Email.to = 5
GROUP BY Employee.id
ORDER BY total_curses DESC
SELECT em.name, sum(s.curse_words) AS total_curses
FROM employee em
JOIN email e ON e.from = em.id
LEFT JOIN emailstats s ON s.email = e.id
WHERE e.to = $the_one_client
GROUP BY em.name
ORDER BY total_curses DESC;
I use a LEFT JOIN to make sure, because there does not seem to be a guarantee, that a matching row in emailstats actually exists.

retrieve records where ALL values in related table are null

I'm looking for a more efficient way to build this query (double subqueries make me cringe):
SELECT contact_id FROM (
SELECT * FROM (
SELECT mr.contact_id, di.district
FROM recipients mr
JOIN address a ON mr.contact_id = a.contact_id
JOIN district_values di ON a.id = di.entity_id
WHERE mr.mid = 29
ORDER BY di.district DESC ) addrSingle
GROUP BY mr.contact_id ) addrNull
WHERE di.district IS NULL
Let me explain what's going on here.
Recipients holds a list of contacts. Each contact may have multiple addresses. Each address has a related district_values table. I need to retrieve contacts where the district_values.district column is null for ALL addresses.
For example:
Contact A
Address 1.district = 4
Address 2.district = null
= don't include
Contact B
Address 1.district = null
= include
Contact C
Address 1.district = null
Address 2.district = 3
= don't include
The logic of my existing query is as follows:
retrieve contacts with related addresses and districts, order so that any addresses with a non null value are ordered first
apply group by so i reduce to a single contact record and if addresses with a district are retained
apply where clause to remove addresses with at least one district value
It works -- it's just a bit ugly.
You could try this, use LEFT JOIN and count the related record which is zero.
SELECT mr.contact_id
FROM recipients mr
LEFT JOIN address a ON mr.contact_id = a.contact_id
LEFT JOIN district_values di ON a.id = di.entity_id
WHERE mr.mid = 29
GROUP BY mr.contact_id
HAVING COUNT(a.*) = 0 AND COUNT(di.*) = 0