I have the 2 following Queries:
This table can have multiple lines per user (many photos) and I just want to return the number/count of photos:
SELECT COUNT(*) FROM photos WHERE photo_description_profanity=1 AND photo_visible=1 AND photo_verified=1 AND userid='1000000002';
This table(s) is only one per user:
SELECT a.userid,a.profile_username,a.profile_gender,a.photo_name,a.photo_verified,b.profile_headline,
YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS(a.profile_birthdate) DAY)) AS age,d.city,c.english AS country
FROM login AS a
JOIN `profiles` AS b ON a.userid=b.userid
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid
WHERE a.userid IN ('1000000002','1000000003','1000000004');
Is it possible to combine these 2 queries?
This is at my limit of ability so any advice would be great :) thx
SELECT
a.userid,
a.profile_username,
a.profile_gender,
a.photo_name,
a.photo_verified,
b.profile_headline,
YEAR(DATE_SUB(NOW(),
INTERVAL TO_DAYS(a.profile_birthdate) DAY)) AS age,
d.city,c.english AS country,
(SELECT COUNT(*) FROM photos WHERE photo_description_profanity=1 AND photo_visible=1 AND photo_verified=1 AND userid= a.userid) AS result_count
FROM login AS a
JOIN `profiles` AS b ON a.userid=b.userid
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid
WHERE a.userid IN ('1000000002','1000000003','1000000004');
It is possible. this takes two steps:
adapt the COUNT(*) into a filtering SUM()
plug the source table into the query
.
SELECT a.userid,a.profile_username,a.profile_gender,a.photo_name,a.photo_verified,b.profile_headline,
YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS(a.profile_birthdate) DAY)) AS age,d.city,c.english AS country
//Here we insert the count column
,IFNULL(SUM(IF(photo_description_profanity=1 AND photo_visible=1 AND photo_verified=1,1,0)),0) AS photocount
FROM login AS a
JOIN `profiles` AS b ON a.userid=b.userid
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid
//Here we insert the source table
LEFT JOIN photos ON photos.userid=a.userid
WHERE a.userid IN ('1000000002','1000000003','1000000004')
//Group by users
GROUP BY a.userid
If you want this as an additional column, then cross join it into the results:
select const.cnt, . . .
from (SELECT COUNT(*) as cnt
FROM photos
WHERE photo_description_profanity=1 AND photo_visible=1 AND
photo_verified=1 AND userid='1000000002'
) const cross join
login AS a
JOIN `profiles` AS b ON a.userid=b.userid
JOIN geoCountry AS c ON a.profile_country=c.countryCode
JOIN geoWorld AS d ON a.profile_geo_location=d.pid
//Here we insert the source table
LEFT JOIN photos ON photos.userid=a.userid
WHERE a.userid IN ('1000000002','1000000003','1000000004');
The advantage of using a cross join instead of a nested select query is performance. MySQL will execute the nested select for every row in the return set. In the from clause, it will only be executed once.
Related
...
Trying to learn SQL and the following query:
SELECT a.id, a.name, w.channel, COUNT(*) use_of_channel
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
GROUP BY a.id, a.name, w.channel
HAVING COUNT(*) > 6 AND w.channel = 'facebook'
ORDER BY use_of_channel;
returns 46 results (first query results), JUST ADDING A JOIN of an unrelated table returns 220 results.
Its not a CROSS JOIN since it seems properly formatted, just added down here at line 5 a JOIN with "orders" table
SELECT a.id, a.name, w.channel, COUNT(*) use_of_channel
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
JOIN orders o
ON o.account_id = a.id
GROUP BY a.id, a.name, w.channel
HAVING COUNT(*) > 6 AND w.channel = 'facebook'
ORDER BY use_of_channel;
...but why would another table compromise the results?
It is a cross join. That is, each account has multiple events. And each account has multiple orders. So within each account you are getting a Cartesian product.
A quick way to fix this is to use count(distinct) on a primary key:
SELECT a.id, a.name, w.channel,
COUNT(w.id) as use_of_channel
I have 2 query:
SELECT CustomerID,count(b.BookingStatus) as 'NotComplete'
FROM Booking b, Customer c
WHERE c.CustomerID=b.BookingCustomerID
AND(b.BookingStatus='Pending'
OR b.BookingStatus='OTW')
GROUP BY c.CustomerID
SELECT c.CustomerID, r.*
FROM Customer c,Regular r
WHERE c.CustomerID=r.RegularCID
Result:
1st query
2nd query
How to combine these 2 result together?
also, display the zero(count) as well.
Thanks!
this is what I get after few hours of trying..obviously it's not what I want..
SELECT c.CustomerID,count(b.BookingStatus) as 'NotComplete',r.RegularID
FROM Booking b, Customer c
JOIN Regular r on r.RegularCID=c.CustomerID
WHERE c.CustomerID=b.BookingCustomerID
AND (b.BookingStatus='Pending'
or b.BookingStatus='OTW'
or b.BookingStatus='Started'
or b.BookingStatus='Unclaimed'
or b.BookingStatus='Confirmed')
GROUP by r.RegularID
You can JOIN to the Regular table and then LEFT JOIN to a derived table of counts in the Booking table. We do it this way to avoid having to GROUP BY all the columns in the Regular table:
SELECT c.CustomerID, r.*, b.NotComplete
FROM Customer c
JOIN Regular r ON r.RegularCID = c.CustomerID
LEFT JOIN (SELECT BookingCustomerID, COUNT(*) AS NotComplete
FROM Booking
WHERE BookingStatus IN ('Pending', 'OTW', 'Started', 'Unclaimed', 'Confirmed')
GROUP BY BookingCustomerID) b ON b.BookingCustomerID = c.CustomerID
Use join on regular table, and subquery on your first select
SELECT t1.*, r.RegularCID FROM (
SELECT CustomerID,count(b.BookingStatus) as 'NotComplete',
FROM Booking b
INNER JOIN Customer c ON c.CustomerID=b.BookingCustomerID
WHERE (b.BookingStatus='Pending' OR b.BookingStatus='OTW')
GROUP BY c.CustomerID) t1
LEFT JOIN Regular r on r.CustomerID = t1.CustomerID
So I have a PHP script that pulls data from two tables at the moment... How could I pull data from a third one, but only get the newest record from that third?
This is my query now:
SELECT c.firstname, c.lastname, r.reminder, r.cid, a.timestamp, a.practitioner FROM `records` AS r
LEFT JOIN `clients` AS c ON (
r.cid = c.id
)
LEFT JOIN `appointments` AS a ON (
r.calid = a.id
);
So what this does right now is get the r.cid (client id) and r.reminder from 'records', then use that r.cid to get the c.firstname, c.lastname from 'clients' and then finally get a.timestamp, a.practitioner from 'appointments'... What I need to do is add a 3rd table to this query, named 'logs' and pull only the newest entry (timestamp column - and actually just pull only this column coz I'm only interested in the date) for each user (cid).
So third table only has cid (same as in other table - user id), timestamp and message. I only need to pull the latest timestamp from this table for each user.
Join with a subquery that gets the maximum log timestamp for each user.
SELECT c.firstname, c.lastname, r.reminder, r.cid, a.timestamp, a.practitioner, l.log_timestamp
FROM `records` AS r
LEFT JOIN `clients` AS c ON (
r.cid = c.id
)
LEFT JOIN `appointments` AS a ON (
r.calid = a.id
)
LEFT JOIN (
SELECT cid, MAX(timestamp) AS log_timestamp
FROM logs
GROUP BY cid) AS l ON l.cid = r.cid
I would like to get the data from one table, and count all results from other table, depending on the first table data, here is what I tried:
SELECT
cars.*, (
SELECT
COUNT(*)
FROM
uploads
WHERE
uploads.cid = cars.customer
) AS `count`,
FROM
`cars`
WHERE
customer = 11;
I dont really have an idea why its not working, as I'm not a regular MySQL user/coder...
Could anyone direct me in the right direction with this one?
SELECT
c.*, COUNT(u.cid) AS count
FROM
cars c
LEFT JOIN
uploads u
ON
u.cid=c.customer
WHERE
u.customer = 11;
GROUP BY c.cid
Try it by joining both tables using LEFT JOIN
SELECT a.customer, COUNT(b.cid) totalCount
FROM cars a
LEFT JOIN uploads b
ON a.customer = b.cid
WHERE a.customer = 11
GROUP BY a.customer
using COUNT(*) in LEFT JOIN will have records to have a minimum count of 1.
SELECT cars.*,COUNT(uploads.*) as uplloaded
from cars
left outer join uploads on uploads.cid = cars.customer
where cars.customer = 11
group by uploads.cid;
Try this :
SELECT customer, COUNT(cid) totalCount
FROM cars
INNER JOIN uploads
ON (customer = cid)
WHERE customer = 11
GROUP BY customer
I have table contracts c (id, exp_date) and table members m (id, cid).
I need to count all contracts and all members together joining the 2 tables on m.cid = c.id.
I tried this but obviously isn't not right as it returns a same count result from both tables
SELECT count(m.id) as totmembers , count(c.id) as totcontracts
from members m
join contracts c on m.cid = c.id
where DATEDIFF(c.im_exp, CURDATE()) > 0
Results should be something like 5000 contracts, 12.000 members but i'm getting 12.000 for both totmembers and totcontracts.
Try this:
SELECT count(m.id) as totmembers , count(distinct c.id) as totcontracts
from members m
join contracts c on m.cid = c.id
where DATEDIFF(c.im_exp, CURDATE()) > 0
Becouse you are created an INNER JOIN statement. Create a new query with two individual query int the SELECT list.
SELECT (SELECT ... WHERE ...) AS totmembers, (SELECT ... WHERE ...) AS totcontracts