I have a problem with a MySQL Query:
I have two tables:
- clustercategories
- domains
Now I have a SQL Query which lists all Domains of a specific category with category name - this is my Query:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
The Problem is now, that I also have a third table "subpages" where I want to count all entries of a specific domain with status = '1' and I don't know how to modify my query to work - I have tried this query, but I do not get any results:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1'))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1')))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
Has anyone any ideas?
I think that you'll want to put your subquery into your projection, like this:
SELECT domains.*, clustercategories.clustercategoryname,
(SELECT COUNT(*) FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1') AS total
FROM domains, clustercategories
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
It looks to me your first query can be rewritten like this
SELECT d.*
, cc.clustercategoryname
FROM domains d
INNER JOIN clustercategories cc
ON cc.id = d.cluster1id
OR cc.id = d.cluster2id
WHERE cc.id IN (3, 10)
AND d.status = '1'
GROUP BY
d.name
ORDER BY
d.name
thus adding the count of subpages can be done like this
SELECT d.*
, cc.clustercategoryname
, sp.total
FROM domains d
INNER JOIN clustercategories cc
ON cc.id = d.cluster1id
OR cc.id = d.cluster2id
LEFT OUTER JOIN (
SELECT COUNT(*) AS total
, domainid
FROM subpages
WHERE subpages.status = '1'
GROUP BY
domainid
) sp ON sp.domainid = d.domainid
WHERE cc.id IN (3, 10)
AND d.status = '1'
GROUP BY
d.name
ORDER BY
d.name
Related
When I execute the following query I receive an Exception:
Error Code: 3065 Expression #1 of ORDER BY clause is not in SELECT
list, references column 'webstore.level_depth' which is not in
SELECT list; this is incompatible with DISTINCT
My Query:
SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
FROM `pj_category_shop` cs, `pj_category` c
INNER JOIN `pj_category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = 1 AND cl.id_shop = 2 )
WHERE (c.`active` = 1 OR c.`id_category` = 2)
AND cs.`id_category` = c.`id_category`
AND cs.`id_shop` = 2
AND c.`id_category` != 1
AND `level_depth` <= 2
AND c.id_category IN (
SELECT id_category
FROM `pj_category_group`
WHERE `id_group` IN (3)
)
ORDER BY `level_depth` ASC, cl.`name` ASC;
Why is this happening?
I have find the answer for my question.Actually mysql 5.7 contains 'ONLY_FULL_GROUP_BY' in sql mode.So we can't perform orderby in the element that is not in select list.we have to change it from
'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
into
'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
We can done this by executing the following queries
SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
ORDER BY column should be column listed in the SELECT list
Add c.level_depth in your select list
Try:
SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite, c.level_depth
FROM `pj_category_shop` cs, `pj_category` c
INNER JOIN `pj_category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = 1 AND cl.id_shop = 2 )
WHERE (c.`active` = 1 OR c.`id_category` = 2)
AND cs.`id_category` = c.`id_category` AND cs.`id_shop` = 2
AND c.`id_category` != 1
AND `level_depth` <= 2
AND c.id_category IN (SELECT id_category FROM `pj_category_group` WHERE `id_group` IN (3))
ORDER BY c.`level_depth` ASC, cl.`name` ASC;
Sql Feature Order by is as the name suggests used to order the Selected Columns on the basis of the Column mentioned in the below Syntax :
Order by Column_Name ASC/DESC
So if you don't add the column using which you have decided to retrieve order set of data in the select clause you will get this error.
SELECT DISTINCT c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
FROM `pj_category_shop` cs, `pj_category` c
INNER JOIN `pj_category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = 1 AND cl.id_shop = 2 )
WHERE (c.`active` = 1 OR c.`id_category` = 2)
ORDER BY c.`level_depth` ASC, cl.`name` ASC
AND cs.`id_category` = c.`id_category` AND cs.`id_shop` = 2
AND c.`id_category` != 1
AND `level_depth` <= 2
AND c.id_category IN (SELECT id_category FROM `pj_category_group` WHERE `id_group` IN (3));
To summarize, you need to have the ORDER BY in the context of the SELECT command, in which case, with the WHERE, FROM and INNER JOIN.
Linking together some threads here - i believe it has to do with mysql version (we were able to bypass by using 5.7) and/or strict mode: https://github.com/publiclab/plots2/pull/8145
Thanks!
There is a way to bypass it. It is not the best practice that you can do ( I think that it is even worse... but if you don't have any control over your SQL_MODE it should work):
SELECT DISTINCT d.id_parent, d.id_category, d.name, d.description, d.link_rewrite
FROM (select c.id_parent, c.id_category, cl.name, cl.description, cl.link_rewrite
FROM `pj_category_shop` cs, `pj_category` c
INNER JOIN `pj_category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = 1 AND cl.id_shop = 2 )
WHERE (c.`active` = 1 OR c.`id_category` = 2)
ORDER BY c.`level_depth` ASC, cl.`name` ASC
AND cs.`id_category` = c.`id_category` AND cs.`id_shop` = 2
AND c.`id_category` != 1
AND `level_depth` <= 2
AND c.id_category IN (SELECT id_category FROM `pj_category_group` WHERE `id_group` IN (3))) as d ;
I need to order a result by number of sold products.
Here is an extract from my (complex) SQL:
SELECT ....
SUM(products.quantity) as numberSold,
....
ORDER by numberSold
How can I order by sold items ONLY during this year ?
I tried with
HAVING products.products.date>'01-01-2015', but it affcts the number of returned rows.
Here is the indigest SQL I did not write and that I have to change:
Select Distinct
boutique_produit.*,
boutique_produit_description.*,
boutique_produit_plus.*,
Sum(boutique_commande_detail.quantite) As numberSold
From
boutique_categorie Inner Join
boutique_categorie_produit On boutique_categorie_produit.boutique_categorie_id
= boutique_categorie.id_boutique_categorie Inner Join
boutique_produit On boutique_produit.id_boutique_produit =
boutique_categorie_produit.boutique_produit_id And
boutique_produit.zone_id = boutique_categorie_produit.zone_id Inner Join
boutique_produit_description
On boutique_produit_description.boutique_produit_id =
boutique_produit.id_boutique_produit And
boutique_produit_description.zone_id = boutique_categorie_produit.zone_id
And (boutique_produit_description.boutique_langue_disponible_code = 'FR')
Inner Join
boutique_produit_reference On boutique_produit_reference.boutique_produit_id =
boutique_produit.id_boutique_produit And boutique_produit_reference.zone_id
= boutique_categorie_produit.zone_id Inner Join
boutique_produit_reference_prix
On boutique_produit_reference_prix.boutique_produit_reference_id =
boutique_produit_reference.id_boutique_produit_reference And
boutique_produit_reference_prix.zone_id = boutique_categorie_produit.zone_id
Inner Join
boutique_taxe On boutique_taxe.id_boutique_taxe =
boutique_produit_reference.boutique_taxe_id Inner Join
boutique_produit_plus On boutique_produit_plus.boutique_produit_id =
boutique_produit.id_boutique_produit And boutique_produit_plus.zone_id =
boutique_categorie_produit.zone_id Inner Join
boutique_produit_plus_categories
On boutique_produit_plus_categories.boutique_produit_id =
boutique_produit_plus.boutique_produit_id And
boutique_produit_plus_categories.zone_id = boutique_produit_plus.zone_id And
(boutique_produit_plus_categories.categorie_id In (1750, 1227, 1880))
Inner Join
poi On boutique_produit_plus.poi_id = poi.ID_poi And poi.zone_id =
boutique_categorie_produit.zone_id And (((poi.payant = 1 And
('2015-12-10' >= poi.dateDebutValidite) And ('2015-12-10' <=
poi.dateFinValidite)) Or (poi.illimite = 1))) Inner Join
boutique_professionnel On poi.boutique_professionnel_id =
boutique_professionnel.id_boutique_professionnel And
boutique_professionnel.zone_id = poi.zone_id And
(boutique_professionnel.compte_actif = 1) Left Join
boutique_commande_detail
On boutique_commande_detail.boutique_produit_reference_id =
boutique_produit_reference.id_boutique_produit_reference And
boutique_commande_detail.zone_id = boutique_categorie_produit.zone_id
Where
boutique_categorie_produit.boutique_categorie_id = 382 And
(boutique_produit_plus.date_fin_valid = '' Or
boutique_produit_plus.date_fin_valid Is Null Or
boutique_produit_plus.date_fin_valid >= '2015-12-10 23:59:59') And
boutique_produit.produit_actif = 1 And
boutique_categorie_produit.zone_id = 4
Group By
boutique_produit.id_boutique_produit, boutique_produit.zone_id
Order By
numberSold Desc
Limit 0, 60
numberSold is the IMPORTANT field
use the WHERE clause on products.date
If I understand you correctly, you want to sum products.quantity only when products.date>'01-01-2015', and if the date is lower you want to exclude that quantity? Then try CASE.
SELECT ....
SUM(CASE WHEN products.date>'01-01-2015' THEN products.quantity
ELSE 0
END) as numberSold,
....
ORDER by numberSold
Assume tables
team: id, title
team_user: id_team, id_user
I'd like to select teams with just and only specified members. In this example I want team(s) where the only users are those with id 1 and 5, noone else. I came up with this SQL, but it seems to be a little overkill for such simple task.
SELECT team.*, COUNT(`team_user`.id_user) AS cnt
FROM `team`
JOIN `team_user` user0 ON `user0`.id_team = `team`.id AND `user0`.id_user = 1
JOIN `team_user` user1 ON `user1`.id_team = `team`.id AND `user1`.id_user = 5
JOIN `team_user` ON `team_user`.id_team = `team`.id
GROUP BY `team`.id
HAVING cnt = 2
EDIT: Thank you all for your help. If you want to actually try your ideas, you can use example database structure and data found here: http://down.lipe.cz/team_members.sql
How about
SELECT *
FROM team t
JOIN team_user tu ON (tu.id_team = t.id)
GROUP BY t.id
HAVING (SUM(tu.id_user IN (1,5)) = 2) AND (SUM(tu.id_user NOT IN (1,5)) = 0)
I'm assuming a unique index on team_user(id_team, id_user).
You can use
SELECT
DISTINCT id,
COUNT(tu.id_user) as cnt
FROM
team t
JOIN team_user tu ON ( tu.id_team = t.id )
GROUP BY
t.id
HAVING
count(tu.user_id) = count( CASE WHEN tu.user_id = 1 or tu.user_id = 5 THEN 1 ELSE 0 END )
AND cnt = 2
Not sure why you'd need the cnt = 2 condition, the query would get only those teams where all of users having the ID of either 1 or 5
Try This
SELECT team.*, COUNT(`team_user`.id_user) AS cnt FROM `team`
JOIN `team_user` ON `team_user`.id_team = `team`.id
where `team_user`.id_user IN (1,5)
GROUP BY `team`.id
HAVING cnt = 2
I have a really long select from my database with many joins. The problem is with counting SUM: without sum, select time is about 3s, but with SUM is about 15s.
Is it possible to optimize my select to obtain a shorter select time?
Here is my code:
SELECT
accomodation.id,
accomodation.aid,
accomodation.title_en,
accomodation.title_url_en,
accomodation.address,
accomodation.zip,
accomodation.stars,
accomodation.picture,
accomodation.valid_from,
accomodation.valid_to,
accomodation.latitude,
accomodation.longitude,
accomodation.city_id AS
accomodation_city_id,
db_cities.id AS city_id,
db_cities.title_en AS city,
db_cities.title_url AS city_url,
db_countries.title_en AS country_title,
db_countries.title_url_en AS country_url,
accomodation_type.class AS accomodation_type_class,
accomodation_review_value_total.value AS review_total,
MIN(accomodation_price.price) AS price_from,
accomodation_rooms.total_persons
FROM
(SELECT aid, MAX(info_date_add) AS max_info_date_add FROM accomodation GROUP BY aid) accomodation_max
INNER JOIN accomodation
ON
accomodation_max.aid = accomodation.aid AND
accomodation_max.max_info_date_add = accomodation.info_date_add
LEFT JOIN db_cities
ON (
db_cities.id = accomodation.city_id OR
(((acos(sin((db_cities.latitude*pi()/180)) * sin((accomodation.latitude*pi()/180)) + cos((db_cities.latitude*pi()/180)) * cos((accomodation.latitude*pi()/180)) * cos(((db_cities.longitude - accomodation.longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) < '20')
JOIN db_countries
ON db_countries.id = accomodation.country_id
LEFT JOIN accomodation_review_value_total
ON accomodation_review_value_total.accomodation_aid = accomodation.aid
LEFT JOIN accomodation_type_value
ON accomodation_type_value.accomodation_id = accomodation.id
LEFT JOIN accomodation_type
ON accomodation_type.id = accomodation_type_value.accomodation_type_id
JOIN accomodation_season
ON (
accomodation_season.accomodation_aid = accomodation.aid AND
( '2013-11-04' BETWEEN accomodation_season.start_date AND accomodation_season.end_date OR '2013-11-05' BETWEEN accomodation_season.start_date AND accomodation_season.end_date ) )
JOIN accomodation_price
ON
accomodation_price.accomodation_aid = accomodation.aid AND
accomodation_price.accomodation_price_type_id = '1' AND
accomodation_price.accomodation_price_cat_id = '1' AND
accomodation_price.price BETWEEN '20' AND '250' AND
accomodation_price.accomodation_season_id = accomodation_season.id
JOIN accomodation_theme_value
ON accomodation_theme_value.accomodation_id = accomodation.id
INNER JOIN
(SELECT
accomodation_id,
SUM(accomodation_rooms.rooms) AS total_rooms,
SUM(accomodation_rooms.beds * accomodation_rooms.rooms) AS total_persons
FROM accomodation_rooms
GROUP BY accomodation_id) accomodation_rooms
ON
accomodation_rooms.accomodation_id = accomodation.id AND
accomodation_rooms.total_persons >= '4'
WHERE
db_countries.title_url_en LIKE '%spain%' AND
db_cities.title_url LIKE '%barcelona%' AND
accomodation_type_value.accomodation_type_id IN (5,10) AND
total_rooms >= '2' AND
accomodation_theme_value.accomodation_theme_id IN (11,12,13) AND
accomodation.stars IN (3,4,5) AND
( accomodation_review_value_total.value >= '4.5' ) AND
db_cities.id = '2416'
GROUP BY accomodation.aid
ORDER BY
CASE
WHEN accomodation.valid_to>=NOW() AND accomodation.valid_from<=NOW() AND MIN(accomodation_price.price) IS NOT NULL THEN 0
WHEN NOW()>accomodation.valid_to AND accomodation.valid_to>'0000-00-00' AND MIN(accomodation_price.price) IS NOT NULL THEN 1
WHEN accomodation.valid_to>=NOW() AND accomodation.valid_from<=NOW() THEN 2
WHEN NOW()>accomodation.valid_to AND accomodation.valid_to>'0000-00-00' THEN 3
ELSE 4 END,
review_total DESC,
accomodation.title_en
LIMIT 10
Please help me with this:
I have a table like:
id_feature id_product id_feature_value
1 1 50
2 1 54
5 1 67
And I want to select from this table like this:
select count(id_product) from table where (id_feature = 1 AND id_feature_value = 50) AND (id_feature = 2 AND id_feature_value = 54) AND (id_feature = 5 AND id_feature_value = 67)
my query must meet the conditions. Like having count(condition) = 3
I don't know how to write this!
Please help me! and sorry my english!
SELECT count( pf.id_product ) AS nr_product, value, fv.id_feature_value, filter
FROM `nk_category_features` cat_f
INNER JOIN `nk_feature_value` fv ON fv.id_feature = '11'
AND fv.value IS NOT NULL
INNER JOIN `nk_product_features` pf ON pf.id_feature = '11'
AND pf.id_feature_value = fv.id_feature_value
INNER JOIN `nk_product` p ON p.id_product = pf.id_product
AND p.product_active = '1'
INNER JOIN `nk_product_features` pf1 ON ( pf1.id_feature = '14'
AND (
pf1.id_feature_value = '21'
) )
WHERE cat_f.id_feature = '11'
AND filter >0
GROUP BY pf.id_feature, pf.id_product
ORDER BY abs( fv.value ) ASC
this is my query that i used now, but i don't like the solution width inner join, inner join on the same table
try this:
select id_product,count(*)
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
To get just the total of products meeting the where clause
select count(*)
from
( select id_product
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
) tmp