mysql search 2 tables with overlap - php

i have 2 tables, see below - profiles is my main/master table
profiles invoices
____________ ___________________
|id Name | |profileid paid |
|============| |===================|
|1 Abraham | | 2 unpaid |
|2 Martin | | 3 unpaid |
|3 John | | 3 paid |
|____________| |___________________|
as can be seen, abraham has 0 invoices, martin has 1 unpaid invoice, and john has 2 invoices; 1 paid, 1 unpaid.
i want to search for:
all profiles with paid invoices (john)
all profiles with unpaid invoices (john & martin)
all profiles with both paid AND unpaid invoice(s) (john)
i can do 1 and 2 fine but im having a problem with step 3.
here is my query for 1;
$query = "SELECT DISTINCT profiles.name
FROM profiles LEFT JOIN invoices ON (profiles.id=invoices.profileid)
AND (invoices.paid='paid' OR invoices.paid='unpaid')
WHERE
IFNULL(invoices.paid, '') LIKE 'paid';
here is my query for 2;
$query = "SELECT DISTINCT profiles.name
FROM profiles LEFT JOIN invoices ON (profiles.id=invoices.profileid)
AND (invoices.paid='paid' OR invoices.paid='unpaid')
WHERE
IFNULL(invoices.paid, '') LIKE 'unpaid';
here is my query for 3;
$query = "SELECT DISTINCT profiles.name
FROM profiles LEFT JOIN invoices ON (profiles.id=invoices.profileid)
AND (invoices.paid='paid' OR invoices.paid='unpaid')
WHERE
IFNULL(invoices.paid, '') LIKE 'paid'
AND IFNULL(invoices.paid, '') LIKE 'unpaid'
;
as mentioned, 1 & 2 work fine, but 3 gives me 0 results.
any help is much appreciated. thanks

You need to select the invoices table 2 times, in order to get where someone has both paid and unpaid. Try something like -
SELECT DISTINCT profiles.name
FROM profiles
LEFT JOIN invoices i1 ON (profiles.id=i1.profileid)
AND (i1.paid='paid')
LEFT JOIN invoices i2 ON (profiles.id=i2.profileid)
AND (i2.paid='unpaid')
WHERE
IFNULL(i1.paid, '') LIKE 'paid'
AND IFNULL(i2.paid, '') LIKE 'unpaid';
see this sqlfiddle example - http://sqlfiddle.com/#!2/ee4e2/4

One possibility is a sub query to count the distinct invoice types associated with a profile
SELECT `profiles`.`name`
FROM `profiles`
WHERE (
SELECT count( DISTINCT `subinvoices`.`paid` )
FROM `invoices` AS `subinvoices`
WHERE `profiles`.`id` = `subinvoices`.`profileid`
AND (`subinvoices`.`paid` = 'paid' OR `subinvoices`.`paid` = 'unpaid')
) = 2

The easiest way to do this is with aggregation:
select p.*, coalesce(InvoiceInfo, 'NONE')
from profiles p left outer join
(select profileid,
(case when sum(paid = 'PAID') > 0 and sum(paid = 'UNPAID') > 0 then 'BOTH'
when sum(paid = 'PAID') > 0 then 'PAID-ONLY'
when sum(paid = 'UNPAID') > 0 then 'UNPAID-ONLY'
end) as InvoiceInfo
from invoices i
)
on p.id = i.profileid
This allows you to get all three types of information at once. You can count the people in each group, for instance, by doing:
select coalesce(InvoiceInfo, 'NONE'), count(*)
from profiles p left outer join
(select profileid,
(case when sum(paid = 'PAID') > 0 and sum(paid = 'UNPAID') > 0 then 'BOTH'
when sum(paid = 'PAID') > 0 then 'PAID-ONLY'
when sum(paid = 'UNPAID') > 0 then 'UNPAID-ONLY'
end) as InvoiceInfo
from invoices i
)
on p.id = i.profileid
group by coalesce(InvoiceInfo, 'NONE')

Related

multiple where conditions with joining

I am stuck in a problem since long.
I have a table for orders of customers, I want to get 4 types of results like
1st: All orders with status=place
then 2nd: All orders with status=place and customer is new (no previous record in order table of this customer),
then 3rd: All orders with status=place and customer have at least 1 order delivered and never returned previous order ,
then 4th: All orders with status=place and customer have atleast 1 order with status=returned
Query for 1st step is working fine, but i got no idea to get other results.I know its like an assignment but i need help.
MY Try for 1st step
SELECT o.order_id, o.customer_id, o.order_status_id,
FROM `order` o
WHERE (o.order_status_id = '1'
AND o.payment_code <> 'paytabs'
AND o.payment_code <> 'pp_express')
ORDER BY o.order_id DESC
This give me perfect result for step one.I am using OpenCart 2.2.0.0
Database Structure
|order_id |status | payment_code |customer_id |
|---------|-------|--------------|------------|
| 10 | place | cod | 5 |
| 11 | delvr | cod | 4 |
| 12 | return| pp_express | 5 |
| 13 |process| paytabs | 2 |
Any help or suggestion will be appreciated.
Ask me for anything you need more.
Not sure to understand your DB structure, but I will try...
Ad 2:
SELECT o.order_id, o.customer_id, o.order_status_id,
FROM `order` o
WHERE (o.order_status_id = '1'
AND o.payment_code <> 'paytabs'
AND o.payment_code <> 'pp_express')
AND o.customer_id NOT IN (SELECT o2.customer_id from order o2 WHERE o2.order_id != o.order_id AND o2.customer_id = o.customer_id)
ORDER BY o.order_id DESC
Accordingly, ad 4:
SELECT o.order_id, o.customer_id, o.order_status_id,
FROM `order` o
WHERE (o.order_status_id = '1'
AND o.payment_code <> 'paytabs'
AND o.payment_code <> 'pp_express')
AND o.customer_id IN (SELECT o2.customer_id from order o2 WHERE o2.order_status = 'returned')
ORDER BY o.order_id DESC
Now for 3:
SELECT o.order_id, o.customer_id, o.order_status_id,
FROM `order` o
WHERE (o.order_status_id = '...?')
AND o.customer_id NOT IN (SELECT o2.customer_id from order o2 WHERE o2.order_status = 'returned')
AND o.customer_id IN (SELECT o3.customer_id from order o3 WHERE o3.order_status = 'delivered')
ORDER BY o.order_id DESC
BTW: I strongly recommend to rename table "order" since "order" is an SQL keyword.
I think you should use Union when you want to do that with one query.
Other ways, when same customer meets 2 contidions, then you will only see one.
https://www.w3schools.com/sql/sql_union.asp
For the 2nd query : you can just add an AND clause to your SQL :
AND user_id = 0
Assuming a new order is inserted with user_id = 0 if this is a new user
queries:
the queries are as per your question u asked and you orders table structure(i am considering that you have not created any column like created_at and updated_at).
select * from orders where status = 'place';
select orders.order_id ,orders.customer_id from orders where status = 'place' GROUP BY order_id, customer_id HAVING COUNT(customer_id) = 1;
select a.order_id, a.customer_id from orders a where a.status in (select b.status from orders b where a.order_id = b.order_id and status <> 'return') and a.status = 'place' GROUP BY a.order_id, a.customer_id HAVING COUNT(a.customer_id) >= 1;
select a.order_id, a.customer_id from orders a where a.status in (select b.status from orders b where a.order_id = b.order_id and status = 'return') and a.status = 'place' GROUP BY a.order_id, a.customer_id HAVING COUNT(a.customer_id) >= 1

MYSQL joining three tables

I have a simple multiple school management system and I am trying to get total number of teachers, and total number of students for a specific school. My table structures are as follows:
teachers
--------------------------
id | schoolid | Name | etc...
--------------------------
1 | 1 | Bob |
2 | 1 | Sarah|
3 | 2 | John |
students
--------------------------
id | schoolid | Name | etc...
--------------------------
1 | 1 | Jack |
2 | 1 | David|
3 | 2 | Adam |
schools
--------------------------
id | Name | etc...
---------------------------
1 | River Park High |
2 | Stirling High |
I can count just all teachers with the following query:
SELECT COUNT(a.id) AS `totalteachers`
FROM teachers a
LEFT JOIN schools b ON a.schoolid = b.id WHERE b.id = '1'
and similarly I can count the number of teachers with the following query:
SELECT COUNT(a.id) AS `totalstudents`
FROM students a
LEFT JOIN schools b ON a.schoolid = b.id WHERE b.id = '1'
I am however struggling with trying to combine these two queries to get a simple result like this:
totalstudents | totalteachers
--------------------------------
2 | 2
I have tried the following:
SELECT COUNT(a.id) as `totalteachers`, COUNT(c.id) as `totalstudents`
FROM teachers a
LEFT JOIN schools b ON a.schoolid = b.id
LEFT JOIN students c ON c.schoolid=b.id WHERE b.id = '5'
You can do something like this
SELECT
id, name, s.total AS totalstudents, t.total AS totalteachers
FROM schools
JOIN (SELECT schoolid, COUNT(id) AS total FROM teachers GROUP BY schoolid)
AS t ON t.schoolid = id
JOIN (SELECT schoolid, COUNT(id) AS total FROM students GROUP BY schoolid)
AS s ON s.schoolid = id
then you can add where id = 2 or whatever to limit the school.
The problem with the multiple left joins is it generates additional records for each teacher to each student; artifically inflating your counts
There's four ways to solve this: (best imo is what Andrew bone did)
Simply select inline without the joins so the counts are not inflated. (most desirable in my mind as it's easy to maintain)
SELECT (SELECT COUNT(a.id) AS `totalteachers`
FROM teachers a
WHERE A.SchoolID = '1') as TotalTeachers
, (SELECT COUNT(a.id) AS `totalstudents`
FROM students a
WHERE a.SchoolID = '1') as TotalStudents
Use subqueries to get the counts first before the joins, then join. Since count will always be 1 a cross join works.
SELECT totalTeachers, totalStudents
FROM (SELECT COUNT(a.id) AS `totalteachers`
FROM teachers a
LEFT JOIN schools b
ON a.schoolid = b.id
WHERE b.id = '1')
CROSS JOIN (SELECT COUNT(a.id) AS `totalstudents`
FROM students a
LEFT JOIN schools b ON a.schoolid = b.id
WHERE b.id = '1')
Use key word distinct within the count so as not to replicate the counts and negate the artificial inflation (least desirable in my mind as this hides the artifical count increase)
SELECT COUNT(distinct a.id) as `totalteachers`, COUNT(distinct c.id) as `totalstudents`
FROM teachers a
LEFT JOIN schools b ON a.schoolid = b.id
LEFT JOIN students c ON c.schoolid=b.id WHERE b.id = '5'
Another way would be to use a window functions, however these are not available in mySQL.
SELECT COUNT(t.id) AS TotalTeachers, COUNT(st.id) AS TotalStudents
FROM schools s
INNER JOIN teachers t
ON s.id = t.schoolid
INNER JOIN students st
ON s.id = st.schoolid
Try this SQL. I havn't try it but it should work.

How to order by COUNT with SUM and MINUS of multi tables

I'm trying to show post by order them with sum of comment and like.
There are three table using in this query post,comment and like
for table like it has column type that keep value like or unlike.
SQL
SELECT (SELECT COUNT(id) AS count_comment
FROM comment WHERE comment.post_id = post.post_id),
(SELECT COUNT(id) AS count_like
FROM like WHERE like.post_id = post.post_id AND like.type = 'like'),
(SELECT COUNT(id) AS count_unlike
FROM like WHERE like.post_id = post.post_id AND like.type = 'unlike'),
post.* FROM post
ORDER BY (count_comment + count_like - count_unlike) DESC;
So, this is an example when it shows on the page
post_id | comment | like | unlike | (comment+like-unlike)
4 | 5 | 3 | 1 | 7
1 | 2 | 3 | 0 | 5
2 | 1 | 1 | 4 | -2
... | ... | ... | ... | ...
My problem is my SQL is very slow, please suggest another way if it can. I've tried to use JOIN but i can't figured out how its SQL should be, please help thanks.
Using a derived table for each of the counts, the query below counts comments, likes, unlikes for each post and then joins the counts to the post table by post_id.
SELECT
p.post_id,
COALESCE(c.comment_count,0) comment_count,
COALESCE(l.like_count,0) like_count,
COALESCE(ul.unlike_count,0) unlike_count,
(COALESCE(c.comment_count,0)
+ COALESCE(l.like_count,0)
- COALESCE(ul.unlike_count,0)) total
FROM post p
LEFT JOIN (
SELECT c.post_id,
COUNT(*) comment_count
FROM comment c
GROUP BY c.post_id
) c ON c.post_id = p.post_id
LEFT JOIN (
SELECT l.post_id,
COUNT(*) like_count
FROM like l
WHERE l.type = 'like'
GROUP BY l.post_id
) l ON l.post_id = p.post_id
LEFT JOIN (
SELECT ul.post_id,
COUNT(*) unlike_count
FROM like ul
WHERE ul.type = 'unlike'
GROUP BY ul.post_id
) ul ON ul.post_id = p.post_id
ORDER BY total DESC

MySql query, counting occurrences of user_id

My query is:
SELECT * FROM (SELECT user_id
FROM goals
LEFT JOIN goal_results ON goals.id = goal_results.goal_id
WHERE goals.enabled = 1 AND validation = 'accepted') AS u
My results are:
| user_id |
| 4 |
| 5 |
| 4 |
| 5 |
| 6 |
I need to get the count of each of them like:
4 > 2
5 > 2
6 > 1
I've tried many kind of queries with subqueries or using distinct but I'm lost and I don't reach my goal.
You can achieve this by using GROUP BY and COUNT. The subquery is not needed.
SELECT goal_results.user_id, COUNT(*) qty
FROM goals
LEFT JOIN goal_results ON goals.id = goal_results.goal_id
WHERE goals.enabled = 1 AND goal_results.validation = 'accepted'
GROUP BY goal_results.user_id
Adding group by will do.
SELECT u.user_id, count(*) FROM (SELECT user_id
FROM goals
LEFT JOIN goal_results ON goals.id = goal_results.goal_id
WHERE goals.enabled = 1 AND validation = 'accepted') AS u
group by u.user_id
You don't need to do this with a subquery:
SELECT g.user_id, count(*)
FROM goals g LEFT JOIN
goal_results gr
ON g.id = gr.goal_id
WHERE g.enabled = 1 AND validation = 'accepted'
GROUP BY g.user_id;
My guess, though, is that you really want:
SELECT g.user_id, count(gr.goal_id)
FROM goals g LEFT JOIN
goal_results gr
ON g.id = gr.goal_id
WHERE g.enabled = 1 AND validation = 'accepted'
GROUP BY g.user_id;
This will return 0 for users that have no goals. The first will return 1 for them.

mysql query within a query with privacy condition check

I have 3 tables.
myMembers
------------------------------------
id | username | privacy
------------------------------------
1 | userA | 0
2 | userB | 1
3 | userC | 0
4 | userD | 1
following
--------------------------------
id | user_id | follower_id
--------------------------------
1 | 2 | 1
posts
-------------------------------------
id | userID | username | statusMsg
--------------------------------------
1 | 4 | userD | Issac Newton is genius
2 | 2 | userB | Newton Saw apple
3 | 3 | userC | Newtonian Physics
4 | 1 | userA | Calculus came from Sir Newton
There is a search field. When a logged in user searches for 'keyword' in table 'posts', I want to omit results from those users who has set his privacy to '1' and WHERE searcher is not following user B.
The query should logically do this.
SELECT * from posts WHERE (match the keyword)
AND (
if (poster's privacy (which is set in myMembers)==1){
if (seacher is following poster){
select this post
}
}
else { select this post
}
)
LIMIT results to 5 rows
So for a keyword "Newton",
if userA is searching, rows 2,3,4 from 'posts' should be returned.
if userD is searching, only rows 1, 3 and 4 from 'posts' should be returned,
based on privacy and following
Edit: Tagging for future searches: IF condition within WHERE Clause in mySql
Please, try this query (also on SQL Fiddle):
SELECT p.id, p.user_id, m.username, m.privacy,
searcher.username "Searcher", p.status_msg
FROM posts p
JOIN members m ON m.id = p.user_id
LEFT JOIN following f ON p.user_id = f.user_id
JOIN members searcher ON searcher.username = 'userA'
WHERE (m.privacy = 0 OR (m.privacy = 1 AND f.follower_id = searcher.id)
OR m.id = searcher.id)
AND p.status_msg LIKE '%New%'
ORDER BY p.id
LIMIT 5;
I removed username field from posts table, as it is redundant. Also, I named tables and columns slightly different, so query might need cosmetic changes for your schema.
The first line in the WHERE clause is the one that you're looking for, it selects posts in the following order:
First posts from members without privacy;
Then posts from members that are followed by the current searcher;
Finally, posts of the member himself.
EDIT:
This query is using original identifiers:
SELECT p.id, p.`userID`, m.username, m.privacy,
searcher.username "Searcher", p.`statusMsg`
FROM posts p
JOIN `myMembers` m ON m.id = p.`userID`
LEFT JOIN following f ON p.`userID` = f.user_id
JOIN `myMembers` searcher ON searcher.username = 'userD'
WHERE (m.privacy = 0 OR f.follower_id = searcher.id OR m.id = searcher.id)
AND p.`statusMsg` LIKE '%New%'
ORDER BY p.id
LIMIT 5;
EDIT 2:
To avoid duplicates in case there're several followers for the user from the posts table, join and filtering conditions should be changed the following way (on SQL Fiddle):
SELECT p.id, p.user_id, m.username, m.privacy,
searcher.username "Searcher", p.status_msg
FROM posts p
JOIN members m ON m.id = p.user_id
JOIN members searcher ON searcher.username = 'userC'
LEFT JOIN following f ON p.user_id = f.user_id
AND follower_id = searcher.id
WHERE (m.privacy = 0 OR (m.privacy = 1 AND f.id IS NOT NULL)
OR m.id = searcher.id)
ORDER BY p.id
LIMIT 5;
Try the following:
SET #my_user_id= 1;
SELECT * FROM posts p
INNER JOIN myMembers m ON p.user_id= m.id
WHERE statusMsg LIKE '%'
AND privacy=0
AND user_id IN (SELECT follower_id FROM following f WHERE f.user_id=#my_user_id)
LIMIT 5
try this:
SELECT a.*
FROM posts a
LEFT JOIN (SELECT user_id
FROM following a1
INNER JOIN myMembers b1
ON a1.follower_id = b1.id
WHERE a1.follower_id = 1 AND
b1.privacy = 1
) b
ON a.userID = b.user_id AND
WHERE a.statusMsg LIKE '%search%' AND
b.user_id IS NULL
LIMIT 5;
or better approach without subquery:
SELECT a.*
FROM posts a
LEFT JOIN myMembers b
ON a.userID = b.id AND
b.privacy = 1
LEFT JOIN following c
ON a.userID = c.user_id AND
c.follower_id = 1
WHERE a.statusMsg LIKE '%search%' AND
b.id IS NULL AND
c.user_id IS NULL
LIMIT 5;
See: A Visual Explanation of SQL Joins

Categories