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')
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
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
I am working on a mobile app that is currently using an ajax call to run a mySQL query remotely and return the data as an array. I want to run the query locally using SQLite 3 an iPad. Below is the mySQL query. I am having trouble getting the query to work in SQLite
This is my working mySQL Query
SELECT
expense_date.expenseDate,
expenses.cost,
SUM(expenses.cost) as total,
expenses.name,
category.category
FROM expenses
INNER JOIN category ON expenses.catID = category.catID
INNER JOIN expense_date ON expenses.dateID = expense_date.dateID
WHERE expense_date.expenseDate BETWEEN '#from#' AND '#tu#'
AND expenses.catID != '1F27C7A1-3E16-5087-A313-A47D286A77A5'
AND expenses.catID != 'DF64C183-5056-A816-1ACA94F902242B61'
AND expenses.userID = '#userID#'
AND expenses.accountID = '#accountID#'
Group By expense_date.expenseDate
UPDATED MySQL Statement. This statement works in mySQL but not in SQLite 3.
SELECT
expense_date.expenseDate,
SUM(expenses.cost) as total
FROM expenses
INNER JOIN expense_date ON expenses.dateID = expense_date.dateID
WHERE expense_date.expenseDate
BETWEEN '2014-08-01' AND '2014-08-31'
AND expenses.catID != '1F27C7A1-3E16-5087-A313-A47D286A77A5'
AND expenses.catID != 'DF64C183-5056-A816-1ACA94F902242B61'
AND expenses.userID = 'C4C73329-5056-A816-1AB4B9FAE08FDB77'
AND expenses.accountID = '46298719-C78A-4CCE-8ADD-A3FD948DF93E'
Group By expense_date.expenseDate
This is the results of the query. 2014-08-21 has two values for that date: 2000.00 and 372.00 but they are added together and returned as a single value for 2014-08-21.
2014-08-01 32.00
2014-08-16 15.00
2014-08-19 513.55
2014-08-21 2372.00
2014-08-22 525.00
2014-08-25 34.55
2014-08-27 52.78
2014-08-31 30.00
I need to convert the MySQL query code to SQLite. I did a google search and found some info about using CASE but I am not sure how to construct the sql statement. Your help is appreciated
Your SQL query actually doesn't make sense in MySQL, because several of the selected values are indeterminate. You might try something like this:
SELECT ed.expenseDate, SUM(e.cost) as total,
group_concat(e.name) as names,
group_concat(c.category) as categories
FROM expenses e INNER JOIN
category c
ON e.catID = e.catID INNER JOIN
expense_date ed
ON e.dateID = ed.dateID
WHERE ed.expenseDate BETWEEN '#from#' AND '#tu#' AND
e.catID <> '1F27C7A1-3E16-5087-A313-A47D286A77A5' AND
e.catID <> 'DF64C183-5056-A816-1ACA94F902242B61' AND
e.userID = '#userID#' AND
e.accountID = '#accountID#'
Group By ed.expenseDate;
This is the SQLite sql statement that solved my problem.
SELECT expense_date.expenseDate,
SUM(expenses.cost) AS total
FROM expenses INNER JOIN expense_date ON expenses.dateID = expense_date.dateID
WHERE expense_date.expenseDate BETWEEN '"+from+"' AND '"+tu+"'
AND expenses.catID <> '1F27C7A1-3E16-5087-A313-A47D286A77A5'
AND expenses.catID <> 'DF64C183-5056-A816-1ACA94F902242B61' Group By expense_date.expenseDate
Thanks for everyones help.
I want to SELECT a field based on a ID value.
Products
PRODUCT_ID Name
19 Chair
20 Table
Product_fields
ID PRODUCT_ID TYPE DESCRIPTION
1 19 C White
2 19 S Modern
3 20 C Black
4 20 S Classic
I need a result like:
Product Type_C Type_S
Chair White Modern
Table Black Classic
I am able to produce this using two LEFT JOINs on the product_fields table but this slows down the query too much. Is there a better way?
Slows down the query how much? What is acceptable?
If you really don't want to use joins (you must have one join), then use views or nested queries. But I don't think they will be any faster, though you can give it a try.
See views at sqlfiddle
select p.PRODUCT_ID, p.Name, f.CDescription, f.SDescription
from Products p
join(
SELECT PRODUCT_ID, Max( CDescription ) as CDescription,
Max( SDescription ) as SDescription
FROM(
select PRODUCT_ID,
case Type when 'C' then Description end as CDescription,
case Type when 'S' then Description end as SDescription
from Fields
) x
group by PRODUCT_ID
) f
on f.PRODUCT_ID = p.PRODUCT_ID;
The complete statement is:
SELECT
NL.product_name,
PRD.product_sku AS product_sku,
CF.virtuemart_product_id AS virtuemart_product_id,
GROUP_CONCAT(distinct CFA.customsforall_value_name
ORDER BY CFA.customsforall_value_name ASC
separator ' | ' ) AS Name_exp_3,
ROUND((((prices.product_price * CALC.calc_value) / 100) + prices.product_price),
2) AS Prijs,
VMCF_L.custom_value AS latijn,
VMCF_T.custom_value AS THT
VMCF_B.custom_value AS Batch
from j25_virtuemart_products AS PRD
LEFT join j25_virtuemart_product_custom_plg_customsforall AS CF ON CF.virtuemart_product_id = PRD.virtuemart_product_id
join j25_virtuemart_product_prices AS prices ON PRD.virtuemart_product_id = prices.virtuemart_product_id
join j25_virtuemart_calcs AS CALC ON prices.product_tax_id = CALC.virtuemart_calc_id
join j25_virtuemart_products_nl_nl AS NL ON NL.virtuemart_product_id = PRD.virtuemart_product_id
LEFT join j25_virtuemart_product_customfields AS VMCF ON VMCF.virtuemart_product_id = PRD.virtuemart_product_id
LEFT join j25_virtuemart_custom_plg_customsforall_values AS CFA ON CFA.customsforall_value_id = CF.customsforall_value_id
LEFT JOIN j25_virtuemart_product_customfields AS VMCF_L ON VMCF.virtuemart_product_id = VMCF_L.virtuemart_product_id AND VMCF_L.virtuemart_custom_id = 16
LEFT JOIN j25_virtuemart_product_customfields AS VMCF_T ON VMCF.virtuemart_product_id = VMCF_T.virtuemart_product_id AND VMCF_T.virtuemart_custom_id = 3
LEFT JOIN j25_virtuemart_product_customfields AS VMCF_B ON VMCF.virtuemart_product_id = VMCF_B.virtuemart_product_id AND VMCF_B.virtuemart_custom_id = 18
WHERE
PRD.product_sku like '02.%'
group by PRD.virtuemart_product_id
order by NL.product_name;
Where the three SELECT results named 'Latijn', 'THT', and 'Batch' are the ones which I compared earlier as the black/white and classic/modern values.
Hope this makes any sense.
As you can see this involves a Virtuemart installation, so I cannot fiddle about to much with the schema.
When I exclude the bottom 3 JOINS and there related FIELDS, the query takes approx 0,5 seconds. With the JOINS and FIELDS included, the query takes almost 19 seconds.
I have created a view from this complete query which I query from my labeling application.
Thanks everyone! With your input I created:
select
NL.product_nameASproduct_name,
PRD.product_skuASproduct_sku,
CF.virtuemart_product_idASvirtuemart_product_id,
group_concat(distinctCFA.customsforall_value_name
order byCFA.customsforall_value_nameASC
separator ' | ') ASName_exp_3,
round((((prices.product_price*CALC.calc_value) / 100) +prices.product_price),
2) ASPrijs,
f.LatijnASLatijn,
f.THTASTHT,
f.BatchASBatch
from
(((((((j25_virtuemart_productsPRD
left joinj25_virtuemart_product_custom_plg_customsforallCFON ((CF.virtuemart_product_id=PRD.virtuemart_product_id)))
joinj25_virtuemart_product_pricespricesON ((PRD.virtuemart_product_id=prices.virtuemart_product_id)))
joinj25_virtuemart_calcsCALCON ((prices.product_tax_id=CALC.virtuemart_calc_id)))
joinj25_virtuemart_products_nl_nlNLON ((NL.virtuemart_product_id=PRD.virtuemart_product_id)))
left joinj25_virtuemart_product_customfieldsVMCFON ((VMCF.virtuemart_product_id=PRD.virtuemart_product_id)))
left joinj25_virtuemart_custom_plg_customsforall_valuesCFAON ((CFA.customsforall_value_id=CF.customsforall_value_id)))
left joinvw_batch_Latijn_THT_groupedfON ((f.virtuemart_product_id=PRD.virtuemart_product_id)))
where
(PRD.product_skulike '02.%')
group byPRD.virtuemart_product_id
order byNL.product_name``
Which takes 1.4 seconds to execute, a whole lot faster then the 19 seconds I started with.