This is the code I have :
$sqlz = "SELECT t1.user_id, t2.status, t2.email
FROM coverages t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
GROUP BY t1.user_id
HAVING COUNT(t1.user_id) =".$value;
I would like to add this "WHERE users.email IS NOT NULL"
When I do add it, it returns a white page / no results. which I know for a fact there are at least 200 results on the db that contain an email and and match that criteria.
this is an example of what I did that did not work:
$sqlz = "SELECT t1.user_id, t2.status, t2.email
FROM coverages t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
WHERE users.email IS NOT NULL
GROUP BY t1.user_id
HAVING COUNT(t1.user_id) =".$value;
I think you need to use t2 (alias) instead of users.
$sqlz = "SELECT t1.user_id, t2.status, t2.email
FROM coverages t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
WHERE t2.email IS NOT NULL
GROUP BY t1.user_id
HAVING COUNT(t1.user_id) = " .$value;
Related
I have two tables table1 and table2. table1 has columns id and table2_id while table2 has id and category. I need to count rows from table1 based on two separate values in table2.category containing value Regular or Special.
I have done this in two queries but I want to know if it is possible in a single sql. My queries are:
"SELECT COUNT(t1.id) AS regular FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = t2.id WHERE t2.category = 'Regular'";
"SELECT COUNT(t1.id) AS special FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = pr.id WHERE t2.category = 'Special'";
Thanks.
EDIT
The second query JOIN should read ON t1.t2_id = t2.id and not ON t1.t2_id = pr.id. Sorry for the confusion that may have caused. Please update/edit your answers/comments accordingly.
Move the Where condition to CASE statement and do the counting
Here is one way using Conditional Aggregate
SELECT
COUNT(case when t2.category = 'Regular' then t1.id end) AS Regular,
COUNT(case when t2.category = 'Special' then t1.id end) AS special
FROM table1 t1
INNER JOIN table2 t2 ON t1.t2_id = pr.id
Where t2.category IN ('Regular','Special' )
Note : I have changed the LEFT JOIN to INNER JOIN because you want to count only when table2.category is 'Regular' or 'Special' so no use of LEFT JOIN here
Instead of
"SELECT COUNT(t1.id) AS regular FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = t2.id WHERE t2.category = 'Regular'";
"SELECT COUNT(t1.id) AS special FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.t2_id = pr.id WHERE t2.category = 'Special'";
you can do this:
select t2.category, count(t1.id)
from table1 t1
left outer join table2
on t1.t2_id = t2.id
group by t2.category
having t2.category in ('Regular', 'Special')
The suggested query groups the joined records, filters the groups and selects the category name and its count.
How can I connect to 4 tables in a single query using forign key IDs?
I know how to connect to two tables.
$sql = "SELECT tb1.id, tb2.name FROM tblA tbl1 LEFT JOIN tblB tbl2 ON tb1.id = tbl2.studentID ORDER BY tbl1.id DESC LIMIT 20";
$statement = $con_db->prepare($sql);
Try like this :
select t1.ID, t2.studentID, t3.aID, t4.bID
from table1 as t1
left join tbl2 as t2 on t2.studentID = t1.id
left join tbl3 as t3 on t3.aID = t1.id
left join tbl4 as t4 on t4.bID = t1.id
here us the query
SELECT *
FROM Table1
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
AND order_id IN
(SELECT DISTINCT t1.order_id
FROM Table2 t1
INNER JOIN table3 t2 ON t1.prod_id = t2.prod_id
WHERE t2.prod_sku LIKE '%D-600%'
AND t1.create_dttm > '2013-02-15 08:28:41')
You are using a sub-query in WHERE clause, that could be the main reason behind slow execution of your query. Try using JOINS instead of sub query.
SELECT t1.*
FROM Table1 t1
INNER JOIN Table2 t2 ON T1.order_id = T2.order_id
AND t2.create_dttm > '2013-02-15 08:28:41'
INNER JOIN table3 t3 ON t2.prod_id = t3.prod_id
AND t3.prod_sku LIKE '%D-600%'
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
And also check for indexes on your tables.
I am trying to count row numbers on an another table and use HAVING to select only rows greater than 0.
SELECT COUNT(t3.ID),t1.ID,t2.sell,t1.date
FROM `album` t1
INNER JOIN `members` t2 ON t2.aID = t1.ID
INNER JOIN `table` t3 ON t3.rID = t1.ID
WHERE t1.date <= '$dt' AND t2.sell = '1'
ORDER BY t1.date DESC
HAVING COUNT(t3.ID) > 0
It doesnt work. where am i wrong ?
Thanks
You need a GROUP BY clause so that MySQL knows how to group the data when you're using aggregate functions such as COUNT.
SELECT COUNT(t3.ID),t1.ID,t2.sell,t1.date
FROM `album` t1
INNER JOIN `members` t2 ON t2.aID = t1.ID
INNER JOIN `table` t3 ON t3.rID = t1.ID
WHERE t1.date <= '$dt' AND t2.sell = '1'
GROUP BY t1.ID,t2.sell,t1.date
HAVING COUNT(t3.ID) > 0
ORDER BY t1.date DESC
For more information, see this.
How can I join three mysql tables which have one common column (id), For example, Select a, b from Table1, select c,d from table2, select e,f from table3, where id=x
Thanks
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1
JOIN table2 t2 ON (t1.id = t2.id)
JOIN table3 t3 ON (t1.id = t3.id)
ORDER BY t1.id;
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM `table1` t1
JOIN `table2` t2 ON t1.id = t2.id
JOIN `table3` t3 ON t1.id = t3.id
WHERE t1.id = x
SELECT `table1`.`a`,`table2`.`c` .....
FROM `table1` JOIN `table2` USING(`id`) JOIN `table3` USING(`id`)
WHERE `id` = x
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t2.id
AND t2.id = t3.id
AND t3.id = x
SELECT col1,col2,col3 (select any col from any table )
FROM t1 INNER JOIN t2,t3
WHERE t1.id = t2.id
AND t1.id = t3.id;
Please try this query:
SELECT product_details.product_id, product_name.pro_name,categories.cat_name
FROM product_details
INNER JOIN product_name
ON product_details.product_id=product_name.id INNER JOIN categories ON product_details.categories_id=categories.id order by product_details.id;