So, I have a simple left join:
SELECT
a.SetNumber,
a.SetID,
COUNT(a.QuantityOwned) AS Pwnd,
b.ImageURL,
COUNT(a.Quantity) AS Cmplt
FROM a
LEFT JOIN b ON a.SetNumber = b.Number
GROUP BY a.SetID
That produces this:
SetNumber 11 21 13
SetID 1 2 1
Pwnd 45 33 50
Cmplt 50 36 50
ImgURL a.jpg b.jpg c.jpg
Which is fine, when I use the data, but I want a >> arrow in my pagination and to do that I would like to get the amount of rows, ie the desired result in this case is:
3
I know I can count rows with COUNT(*) in one table, but how do I do it in a left join?
I guess you need to do that
SELECT COUNT(1) FROM (SELECT a.SetNumber, a.SetID, COUNT(a.QuantityOwned) AS Pwnd ,b.ImageURL, COUNT(a.Quantity) AS Cmplt
FROM a
LEFT JOIN b ON a.SetNumber = b.Number
GROUP BY a.SetID) table1
The left join is'nt the problem here, it's the group by
But a lot of "Libraries" Allows to count the result count like
$result->count();
In my opinion if you use JAVA, php etc... you don't need to do another request
Related
So guys, trying to write a query to get the count of statuses where project_id = ? and statuses in 'New' from a couple of tables so let me break it down.
I have these three tables
Case_Status
id case_status
1 New
2 Failed
3. Accepted
Referral
id case_status_id project_id application_id
1 1 1 20
2 2 1 21
Project
id name
1 project1
2 project2
So this is my query
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM "case_statuses" LEFT OUTER JOIN "referrals" ON "referrals"."case_status_id" = "case_statuses"."id"
WHERE "case_statuses"."deleted_at" IS NULL AND (case_statuses.case_status IN ('New') AND referrals.project_id = 1)
GROUP BY case_statuses.case_status;
This is my result
count_all counted
1 New
1 Failed
But I am expecting this result instead
count_all counted
1 New
1 Failed
0 Accepted
Does anyone know what's wrong with my query that isnt showing count for all the case_statuses?
Thanks
Conditions on the second table (in a left join) should be in the on clause:
SELECT COUNT(r.id) AS count_all, cs.case_status AS counted
FROM case_statuses cs LEFT OUTER JOIN
referrals r
ON r.case_status_id = cs.id AND r.project_id = 1
WHERE cs.deleted_at IS NULL AND cs.case_status NOT IN ('New')
GROUP BY cs.case_status;
Otherwise, the WHERE clause turns the outer join into an inner join.
change your query like this
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM "case_statuses" LEFT JOIN "referrals" ON "referrals"."case_status_id" = "case_statuses"."id" AND referrals.project_id = 1
WHERE "case_statuses"."deleted_at" IS NULL AND case_statuses.case_status NOT IN ('New')
GROUP BY case_statuses.case_status;
Given your data and the expected result you just need to loose the WHERE clause.
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM case_statuses
LEFT OUTER JOIN referrals ON referrals.case_status_id = case_statuses.id
GROUP BY case_statuses.case_status;
See this fiddle for details.
So i have a database with some tables. Now i want a query that gets data from 3 tables. First lets see what the databases are
omschrijvingVoorraad
-ID 1
-userID 1
-omschrijvingID 6
-min 4
omschrijving
-ID 6
-omschrijving Cola (blikje 330ml)
voorraad
-ID 20
-userID 1
-omschrijvingID 6
-aantal 2
Now i want to make a query that will show the next line:
Cola (blikje 330ml) Aantal 2 minmaal 4
I searched around and came up with below but it is not working. It doesn't give an error but just an empty result
$queryOm="SELECT omschrijvingVoorraad.ID, omschrijvingID, omschrijving, vAantal, min
FROM omschrijvingVoorraad
LEFT JOIN omschrijving ON omschrijving.ID = omschrijvingVoorraad.omschrijvingID
INNER JOIN ( SELECT omschrijvingID vid, SUM( aantal ) vAantal
FROM voorraad WHERE userID='$userID' ) p ON vid = omschrijvingVoorraad.omschrijvingID
WHERE userID='$userID'
LIMIT $offset, $perPage";
Where offcourse the $offset and $perPage are being defined earlier in the code.
So can anyone tell me where I went wrong? What should I change to get the correct result?
Looking to your schema and your expected result seem you need this query
select a.ID, a.omschrijvingID, b.omschrijving, sum(c.aantal), a.min
from omschrijvingVoorraad as a
inner join omschrijving as b on a.omschrijvingID = b.ID
inner join voorraad as c on a.omschrijvingID = c.omschrijvingID
group by a.ID, a.omschrijvingID, b.omschrijving, a.min
I have a query where I try to retrieve the number of rows but in my case drops as result 1 on each time. I suppose that is a join problem and I don't kniw how to handle this
SELECT
COUNT(*) AS summe
FROM
feeds
LEFT JOIN cat_product cp
ON cp.product_id = feeds.productid
WHERE 1 = 1
AND cp.price BETWEEN 950
AND 1450
AND cp.ram_cap BETWEEN 5
AND 11
AND feeds.tdcategoryname LIKE '%Laptop%'
AND feeds.brand IN ('Dell', 'Lenovo', 'Acer', 'Asus')
GROUP BY cp.product_id
Try this, not sure if you are using MySQL or MSSQL. Since you LEFT JOIN there is a possibility that a value is NULL. In MSSQL if a NULL value is accounted for the the result will be NULL Always when using a aggregated function like COUNT.
For MySQL
SELECT
COUNT(IFNULL(cp.Procuct_id,0)) AS summe
FROM
feeds
LEFT JOIN cat_product cp
ON cp.product_id = feeds.productid
WHERE 1 = 1
AND cp.price BETWEEN 950
AND 1450
AND cp.ram_cap BETWEEN 5
AND 11
AND feeds.tdcategoryname LIKE '%Laptop%'
AND feeds.brand IN ('Dell', 'Lenovo', 'Acer', 'Asus')
I have three tables: T_O, T_C, & T_D they each have a date (run_date) and count column.
I need to select the data to show a summary by date for each count. I cannot get the nested sql to make it look right.
Needs to be grouped by Run Date. End result should look like this:
Run_Date Total Defects Not Closed Closed
05/29/13 178 100 78
06/04/13 204 103 101
06/11/13 234 114 120
I assume the three tables each have two columns: Run_Date and Total Defects, Not Closed or Closed. If you have a table with all the run dates in it, something like this will work:
SELECT
RunDates.Run_Date,
T_D.[Total Defects],
T_O.[Not Closed],
T_C.[Closed]
FROM ((
RunDates
LEFT JOIN T_C ON RunDates.Run_Date = T_C.Run_Date)
LEFT JOIN T_O ON RunDates.Run_Date = T_O.Run_Date)
LEFT JOIN T_D ON RunDates.Run_Date = T_D.Run_Date
If not, you will need to construct one using a UNION (not UNION ALL):
SELECT
RunDates.Run_Date,
T_D.[Total Defects],
T_O.[Not Closed],
T_C.[Closed]
FROM ((
(SELECT Run_Date FROM T_C
UNION
SELECT Run_Date FROM T_O
UNION SELECT Run_Date FROM T_D) AS RunDates
LEFT JOIN T_C ON RunDates.Run_Date = T_C.Run_Date)
LEFT JOIN T_O ON RunDates.Run_Date = T_O.Run_Date)
LEFT JOIN T_D ON RunDates.Run_Date = T_D.Run_Date
(The example that follows is hypothetical, but illustrates the concept).
Using MySQL, say I have 2 tables:
userFromID userToId moreInfo
1 2 cat
1 3 dog
4 1 bear
3 4 fish
And...
userId someInfo addlInfo
1 m 32
2 f 33
3 m 25
4 f 28
And I want to query for a user id, and get back joined info from both tables for all users that share a relationship with user1.
assume that the first table has something like alter table thatFirstTable add unique index(userFromId, userToId) so there won't be any duplicates - each relationship between the two ids will be unique.
it doesn't matter who's the "from" or "to"
so the desired result would be something like this, if queried for relationships with user id: 1
userId moreInfo someInfo addlInfo
2 cat f 33
3 dog m 25
4 bear f 28
Thanks.
/EDIT this "works" but I suspect there's a better way?
SELECT * FROM users JOIN friends ON friends.userFrom = users.id OR friends.userTo = users.id WHERE users.id != 1 AND friends.userFrom = 1 OR friends.userTo = 1
/EDIT2 - I updated the sample output to better reflect the goal
try this query::
select tbl2.userid,tbl1.moreinfo,
tbl2.someinfo,tbl2.addinfo
from tbl1 join tbl2
on (tbl1.usertoid = tbl2.userid and tbl1.userfromid = 1)
You should just join the tables with the query below.
select u.userId, f.moreInfo, u.someInfo, u.addlInfo
from users AS u INNER JOIN friends AS f ON u.userId = f.UserToId
where f.userFrom = 1
Try this. Tested and 100% working
select a.userToID, a.moreInfo, b.someInfo, b.addInfo from tbl1 a
left outer join
tbl2 b on a.userToID = b.userId
where a.userFromID = 1;