I have below conditional query to perform multiple columns searches
SELECT
prod.pid,
prod.pname,
prod.description,
prod.status,
foto.set_cover,
foto.filename
FROM products prod
INNER JOIN second_child_categories secd ON prod.category_id = secd.second_id
INNER JOIN photos foto ON prod.pid = foto.pid
WHERE CONCAT( prod.pname, prod.description ) LIKE '%home%'
OR CONCAT( prod.description, prod.pname ) LIKE '%home%'
AND foto.set_cover = '1'
AND prod.status = '1'
ORDER BY prod.created_date DESC
Here's the output:
As refer to query, I set conditions:
foto.set_cover='1' AND prod.status='1'
the results are not matches with the condition.
How can I fix this query?
Thanks.
See Mysql or/and precedence?
ORhas a higher precedence, what you (implicitly) wrote:
CONCAT( prod.pname, prod.description ) LIKE '%home%'
OR (
CONCAT( prod.description, prod.pname ) LIKE '%home%'
AND foto.set_cover = '1'
AND prod.status = '1'
)
what I think what you meant
(
CONCAT( prod.pname, prod.description ) LIKE '%home%'
OR
CONCAT( prod.description, prod.pname ) LIKE '%home%'
)
AND foto.set_cover = '1'
AND prod.status = '1'
Related
If this is a dupe, I apologize. I couldn't find it.
I have this convoluted query that hits 3 tables, and I need to figure out how to exclude an entire result if the GROUP_CONCAT as shows returns NULL (which it does often). To clarify, if shows comes up NULL, then I don't want any of the profile data for that result either. I want the entire row skipped.
SELECT
`profiles`.*,
IF( `profiles`.`profile_id` IN(
SELECT DISTINCT (`badges`.`profile_id`)
FROM `badges`
), TRUE, FALSE ) AS `has_registrations`,
(SELECT
GROUP_CONCAT( DISTINCT `shows`.`show_name` SEPARATOR '<br>' )
FROM `shows`
LEFT JOIN `badges` ON `badges`.`show_id` = `shows`.`show_id`
WHERE
`badges`.`profile_id` = `profiles`.`profile_id`
AND `shows`.`end_date` >= CURDATE()
) AS `shows`
FROM `profiles`
Also accepting input on speed/elegance of the overall query.
Probably there is a more efficient way to do this but I think a simple where clause will do the trick.
SELECT
`profiles`.*,
IF( `profiles`.`profile_id` IN(
SELECT DISTINCT (`badges`.`profile_id`)
FROM `badges`
), TRUE, FALSE ) AS `has_registrations`,
(SELECT
GROUP_CONCAT( DISTINCT `shows`.`show_name` SEPARATOR '<br>' )
FROM `shows`
LEFT JOIN `badges` ON `badges`.`show_id` = `shows`.`show_id`
WHERE
`badges`.`profile_id` = `profiles`.`profile_id`
AND `shows`.`end_date` >= CURDATE()
) AS `shows`
FROM `profiles`
where `shows` IS NOT NULL
You could add a having clause:
SELECT
`profiles`.*,
IF( `profiles`.`profile_id` IN(
SELECT DISTINCT (`badges`.`profile_id`)
FROM `badges`
), TRUE, FALSE ) AS `has_registrations`,
(SELECT
GROUP_CONCAT( DISTINCT `shows`.`show_name` SEPARATOR '<br>' )
FROM `shows`
LEFT JOIN `badges` ON `badges`.`show_id` = `shows`.`show_id`
WHERE
`badges`.`profile_id` = `profiles`.`profile_id`
AND `shows`.`end_date` >= CURDATE()
) AS `shows`
FROM `profiles`
having shows is not null
If i understand questions correctly, you don't need null values. Perhaps the null value occurs due use of left join why don't you try with inner join their will be no null values.
I'm trying to get some data from my db.
It kinda looks like this
GROUPS
groups_id, groups_name, groups_description, groups_active, groups_hash, groups_entry_date, user_id, groups_email, groups_sms
CUSTOMERS_GROUPS
customers_hash, groups_hash
CUSTOMERS
customers_id, customers_first_name, customers_surname, customers_telephone, customers_email, customers_telephone_active, customers_email_active, client_type, customers_hash, customers_entry_date
I want customers.groups_hash and groups.groups_name in a concat form. Here is my attempt ...
SELECT * , GROUP_CONCAT( DISTINCT customers_groups.groups_hash
SEPARATOR '/' ) , GROUP_CONCAT( groups.groups_name
SEPARATOR '/' )
FROM customers
INNER JOIN customers_groups ON ( customers.customers_hash = customers_groups.customers_hash )
LEFT JOIN groups ON ( customers_groups.customers_hash = groups.groups_hash )
WHERE groups.groups_active ='1' GROUP BY customers.customers_entry_date
but it gives me back a zero set ...
The problem is your where clause. It must be part of the on clause:
SELECT * , GROUP_CONCAT( DISTINCT customers_groups.groups_hash
SEPARATOR '/' ) , GROUP_CONCAT( groups.groups_name
SEPARATOR '/' )
FROM customers
INNER JOIN customers_groups ON ( customers.customers_hash = customers_groups.customers_hash )
LEFT JOIN groups ON ( customers_groups.customers_hash = groups.groups_hash ) AND groups.groups_active ='1'
GROUP BY customers.customers_entry_date
The problem is here:
LEFT JOIN groups ON ( customers_groups.customers_hash = groups.groups_hash )
which should probably be
LEFT JOIN groups ON ( customers_groups.groups_hash = groups.groups_hash )
(While it remains unclear what the hashes actually represent and why there is no bridge table linking the tables' IDs instead. I've asked that question in the comment section to your request.)
I almost spent a day to optimize this query:
SELECT
prod. *,
cat.slug category_slug,
sup.bname bname,
sup.slug bname_slug
FROM bb_admin.bb_directory_products AS prod
LEFT JOIN bb_admin.bb_categories_products AS cat
ON prod.primary_category_id = cat.category_id
LEFT JOIN bb_admin.bb_directory_suppliers AS sup
ON prod.supplier_id = sup.supplier_id
LEFT JOIN bb_admin.bb_directory_suppliers AS credit_sup
ON prod.credit_supplier_id = credit_sup.supplier_id
LEFT JOIN bb_admin.bb_directory_suppliers AS photo_sup
ON prod.photo_supplier_id = photo_sup.supplier_id
WHERE (
prod.status = '1'
OR prod.status = '3'
OR prod.status = '5'
)
AND (
sup.active_package_id != '1'
OR sup.active_package_id != '5'
OR sup.active_package_id != '6'
OR prod.supplier_id = '0'
)
AND (
sup.supplier_id = '1989'
OR credit_sup.supplier_id = '1989'
OR photo_sup.supplier_id = '1989'
)
GROUP BY prod.product_id
ORDER BY prod.priority_index ASC
Can you help me to optimized this query?
Update your column data types to be INT or one of its variants, since the ones you are checking against are all integer IDs (assumption).
Create indexes on following columns(if possible in all tables):
prod.status
supplier_id
active_package_id
Use IN clause instead of concatenating OR segments.
I'll be putting the updated WHERE clause here:
WHERE prod.status IN(1, 3, 5)
AND ( sup.active_package_id NOT IN(1, 5, 6)
OR prod.supplier_id = 0
)
AND 1989 IN (prod.supplier_id, prod.credit_supplier_id, prod.photo_supplier_id)
I am trying to bring back a string based on an IF statement but it is extremely slow.
It has something to do with the first subquery but I am unsure of how to rearrange this as to bring back the same results but faster.
Here is my SQL:
SELECT IF
(
(
SELECT COUNT(*)
FROM
(
SELECT DISTINCT enquiryId, type
FROM parts_enquiries, parts_service_types AS pst
WHERE parts_enquiries.serviceTypeId = pst.id
) AS parts
WHERE parts.enquiryId = enquiries.id
) > 1, 'Mixed',
(
SELECT DISTINCT type
FROM parts_enquiries, parts_service_types AS pst
WHERE parts_enquiries.serviceTypeId = pst.id AND enquiryId = enquiries.id
)
) AS partTypes
FROM enquiries,
entities
WHERE enquiries.entityId = entities.id
How can I make it faster?
I have modified my original query below, but I am getting the error that subquery returns more than one row:
SELECT
(SELECT
CASE WHEN COUNT(DISTINCT type) > 1 THEN 'Mixed' ELSE `type` END AS type
FROM parts_enquiries
INNER JOIN parts_service_types AS pst ON parts_enquiries.serviceTypeId = pst.id
INNER JOIN enquiries ON parts_enquiries.enquiryId = enquiries.id
INNER JOIN entities ON enquiries.entityId = entities.id
GROUP BY enquiryId) AS partTypes
FROM enquiries,
entities
WHERE enquiries.entityId = entities.id
Please have a look if this query yields the same results:
SELECT
enquiryId,
CASE WHEN COUNT(DISTINCT type) > 1 THEN 'Mixed' ELSE `type` END AS type
FROM parts_enquiries
INNER JOIN parts_service_types AS pst ON parts_enquiries.serviceTypeId = pst.id
INNER JOIN enquiries ON parts_enquiries.enquiryId = enquiries.id
INNER JOIN entities ON enquiries.entityId = entities.id
GROUP BY enquiryId
But N.B.'s comment is still valid. To see if and index is used and other information we need to see the EXPLAIN and the table definitions.
This should get you what you want.
I would first pre-query your parts enquiries and parts service types looking for both the count and MINIMUM of the part 'type', grouped by the enquiry ID.
then, run your IF() against that result. If the distinct count is > 0, then 'Mixed'. If only one, since I did the MIN(), it would only have the description of that one value that you desire anyhow.
SELECT
E.ID
IF ( PreQuery.DistTypes > 1, 'Mixed', PreQuery.FirstType ) as PartType
from
Enquiries E
JOIN ( SELECT
PE.EnquiryID,
COUNT( DISTINCT PE.ServiceTypeID ) as DistTypes,
MIN( PST.Type ) as FirstType
from
Parts_Enquiries PE
JOIN Parts_Service_Types PST
ON PE.ServiceTypeID = PST.ID
group by
PE.EnquiryID ) as PreQuery
ON E.ID = PreQuery.EnquiryID
I'm a nubie in mysql queries, and I would like to ask, whether if there's someone who can help me to solve my problem. I have this complex(for me) sql. and I want to change it into a view.
SELECT
username,
user_id,
sum(result_points) AS count_points,
count(result_points) AS activity_done,
(
(
SELECT count(*) FROM `activity` WHERE periode_id = ''
) +
(
SELECT
IFNULL(sum(acs.result_points), 0)
FROM
user_activity ua
WHERE
periode_id = ''
AND ua.user_id = user_activity.user_id
AND ua.result_points IS NOT NULL
)
) AS total
FROM
user_activity
LEFT JOIN users ON users.id = user_activity.user_id
WHERE
periode_id = ''
and user_id > 0
GROUP BY user_id
ORDER BY total DESC
is there a way, to take out the "where" statement,so that i still can change it to view?
If you query is correct, create view like
Create View data as
SELECT
username,
user_id,
sum(result_points) AS count_points,
count(result_points) AS activity_done,
(
(
SELECT count(*) FROM `activity` WHERE periode_id = ''
) +
(
SELECT
IFNULL(sum(acs.result_points), 0)
FROM
user_activity ua
WHERE
periode_id = ''
AND ua.user_id = user_activity.user_id
AND ua.result_points IS NOT NULL
)
) AS total
FROM
user_activity
LEFT JOIN users ON users.id = user_activity.user_id
WHERE
periode_id = ''
and user_id > 0
GROUP BY user_id
ORDER BY total DESC
Remove where clause from view and add where clause in view, like
select * from data where user_id > 0