I have a table of following structure:
I want a mysql query to select the category where subcategory = 2171.
Here i want to fetch categories having subcategories ["2171","2172"] and ["2171"] which means that subcategories that includes "2171".
Try:
SELECT category FROM myTable WHERE subcategory LIKE( '%"2171"%' )
Putting the double quotes around it will stop false matches on something like 21717.
Related
i have an table with data which has both category and subcategory data in it.
So what is category and sub-category? category is the parent and sub-category is the child. Both data is similar except for one difference, parent_id for sub_category is id of the category.
Sample Data:
What is required?
sub-category name in the result should be category-name +'-'+ sub-category-name
Sample Result:
What is my attempt to solve the problem?
MySQL query which i have returning is done only to extract the sub-category only with the names:
SELECT
CONCAT(wcfc1.feed_category_name,
'-',
wcfc2.feed_category_name) AS feed_category_name,
wcfc2.feed_category_id,
wcfc2.parent_id,
wcfc2.category_type_id
FROM
wc_feed_categories wcfc1
JOIN
wc_feed_categories wcfc2 ON wcfc1.feed_category_id = wcfc2.parent_id;
This query is showing only:
i'm not able to figure out, how to write a query to show both category and sub-category with the name change in a single query.
Instead of
JOIN
wc_feed_categories wcfc2 ON wcfc1.feed_category_id = wcfc2.parent_id;
try
LEFT JOIN
wc_feed_categories wcfc2 ON wcfc1.parent_id = wcfc2.feed_category_id;
so your query will be:
SELECT
CASE
WHEN wcfc1.parent_id = 0 THEN wcfc1.feed_category_name
ELSE
CONCAT(wcfc2.feed_category_name, -- parent category
'-',
wcfc1.feed_category_name) -- child category
END AS feed_category_name,
wcfc1.feed_category_id,
wcfc1.parent_id
FROM
wc_feed_categories wcfc1
LEFT JOIN
wc_feed_categories wcfc2 ON wcfc1.parent_id = wcfc2.feed_category_id;
Demo here
I am writing a SQL query and I have an array in one table and in that array I stores IDs and want to compare that array of IDs with another table to show data against that ids. when I run it gives me following error.
No database selected Select the default DB to be used by double-clicking.
Here is my query
select TagId
, Name
from ctrData2.Tag
Left
outer join ctrData2.CallDetail
On Tag.TagId = array(CallDetail.Tag)
where CallDetail.ContactId = 'f9d4787a-f1ac-41af-97d8-ea324daad018'
this is how I store IDs in array in Tag Column
here is the other table from where I want to show data against these ids
you can set the default schema by below query first
use ctrData2;
And run below query
select TagId, Name
from Tag, CallDetail
where CallDetail.ContactId = 'f9d4787a-f1ac-41af-97d8-ea324daad018' and LOCATE(Tag.TagId, CallDetail.Tag) > 0;
I have written the following function to display a table consisting of
recipe name, description, cusine , ........ ..., ingredients AND LIKES.
Everything works fine but the likes column doesnot show the no. of likes bu instead sows the no. of ingredients of the recipe
public function rec(){
$ing = inglist::all();
$rcusine=recipecusine::all();
$rtype=recipetype::all();
$rec = DB::table('recipe_list')
->select('recipe_list.recipe_id','recipe_list.Recipe_name',
'recipe_list.Recipe_desc','recipe_list.Recipe_duration',
DB::raw('group_concat(ing_list.Ing_name separator ",") as recipe_ingredients'),
'recipe_cusine.Cusine_name', 'recipe_type.Recipe_type_name','recipe_list.image',
DB::raw('count(likerecipes.likecount) as likes'))
->join('recipe_inglist', 'recipe_list.recipe_id','=','recipe_inglist.Recipe_id')
->join('ing_list', 'recipe_inglist.Ing_id','=','ing_list.ing_id')
->join('recipe_cusine', 'recipe_list.Recipe_cusine_id','=','recipe_cusine.cusine_id')
->join('recipe_type', 'recipe_list.Recipe_type_id','=','recipe_type.Recipe_typeID')
->join( 'likerecipes', 'recipe_list.recipe_id', '=', 'likerecipes.recipe_id')
->where('recipe_list.recipe_id','>=','1')
->groupBy('recipe_list.recipe_id', 'recipe_list.recipe_name','recipe_list.recipe_desc','recipe_list.recipe_duration', 'recipe_cusine.Cusine_name','recipe_type.Recipe_type_name','recipe_list.image' )->get() ;
/* var_dump($rec);
die();*/
return view('recipe', ['ingredients'=>$ing, 'cusine'=>$rcusine, 'type'=>$rtype,'recipe'=>$rec]);
}
the output i get is
and this is my likerecipes table
can anyone help me where i am wrong.
Because of the joins, your data is duplicated through the records. Since the likerecipes table seems to store like votes casted one by one identified by an id field uniquely, I would count the distinct likerecipes.id values per group:
'count(distinct likerecipes.id) as likes'
I would also introduce the distinct to the group_concat() to make sure that multiple like votes do not make the same ingridients appear multiple times:
'group_concat(distinct ing_list.Ing_name separator ",") as recipe_ingredients'
I have a list of categories that are saved in a string. One is on an article table the other on an ad table. I need the script to return all rows that have a combination of any of these categories between the two tables.
Category string on both tables:
Civic,Community,Sports,Business
And my MySQL script
SELECT `Ad_ID`, `Ad_URL`, `Ad_Image`, `Ad_Type`, `Ad_Cities`, `Ad_Categories`
FROM `Ad_TABLE`
INNER JOIN `Article_TABLE` ON `Article_TABLE`.`Article_Cat` = `Ad_TABLE`.`Ad_Cat`
WHERE Article_TABLE.Article_Cat LIKE '%Civic%'
OR Article_TABLE.Article_Cat LIKE '%Community%'
OR Article_TABLE.Article_Cat LIKE '%Sports%'
OR Article_TABLE.Article_Cat LIKE '%Business%'
AND Ad_TABLE.Ad_Cat LIKE '%Civic%'
OR Ad_TABLE.Ad_Cat LIKE '%Community%'
OR Ad_TABLE.Ad_Cat LIKE '%Sports%'
OR Ad_TABLE.Ad_Cat LIKE '%Business%'
The script only returns records that are only in one of these categories, but there are records that are in multiple categories and I need it to return those as well.
How can I get it to where it finds all matching categories between the two tables?
I suspect you need to add parenthesis to the WHERE clause:
SELECT `Ad_ID`, `Ad_URL`, `Ad_Image`, `Ad_Type`, `Ad_Cities`, `Ad_Categories`
FROM `Ad_DB`
INNER JOIN `Article_DB` ON `Article_DB`.`Article_Cat` = `Ad_DB`.`Ad_Cat`
WHERE (Article_DB.Article_Cat LIKE '%Civic%'
OR Article_DB.Article_Cat LIKE '%Community%'
OR Article_DB.Article_Cat LIKE '%Sports%'
OR Article_DB.Article_Cat LIKE '%Business%')
AND (Ad.DB_Cat LIKE '%Civic%'
OR Ad.DB_Cat LIKE '%Community%'
OR Ad.DB_Cat LIKE '%Sports%'
OR Ad.DB_Cat LIKE '%Business%')
I'm not sure exactly how your tables are structured, but this query will return rows where Article_DB.Article_Cat AND Ad.DB_Cat contain one of your categories. You likely want to rethink the way you store data in these tables so that you're not duplicating data.
Here's a guess. This is just a guess, based on a possible interpretation of the nebulous specification.
To "match" rows that have one or more of these four categories in common
Civic,Community,Sports,Business
We could use a query with join predicates like this:
SELECT ...
FROM `Ad_DB` d
JOIN `Article_DB` r
ON ( FIND_IN_SET('Civic' ,d.ad_categories)
AND FIND_IN_SET('Civic' ,r.article_cat )
)
OR ( FIND_IN_SET('Community' ,d.ad_categories)
AND FIND_IN_SET('Community' ,r.article_cat )
)
OR ( FIND_IN_SET('Sports' ,d.ad_categories)
AND FIND_IN_SET('Sports' ,r.article_cat )
)
OR ( FIND_IN_SET('Business' ,d.ad_categories)
AND FIND_IN_SET('Business' ,r.article_cat )
)
ORDER BY ...
NOTE: I understand that we get what we get, and sometimes we get handed a database that stores comma separated lists.
Storing comma separated lists in a column is a SQL Antipattern. For anyone that has an interest as to why it's an antipattern, I recommend Chapter 2 of Bill Karwin's excellent book
https://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
I need to build a query with multiple JOIN, to be more especific, 2 JOINS, but it gets duplicated results, check this:
My current tables:
food_shops
id, slug, name
categories_food_shops
id, id_cat, id_food_shop
pictures_food_shops
id, pic_slug, id_food_shop
And I need to get * from food_shops, the id_cat from categories_food_shops and pic_slug from pictures_food_shops...
My current query is like this:
SELECT food_shops.id, food_shops.slug, food_shops.name,
GROUP_CONCAT(categories_food_shops.id_category) as categories,
GROUP_CONCAT(pictures_food_shops.slug) as pictures
FROM
food_shops
JOIN categories_food_shops
ON food_shops.id = categories_food_shops.id_food_shop
JOIN pictures_food_shops
ON food_shops.id = pictures_food_shops.id_food_shop
GROUP BY food_shops.slug, pictures_food_shops.id_food_shop
But since my pictures_food_shops have more results as the categories_food_shops, my result is gettin "quadruplicated":
What can I do to prevent this and get only the correct amount of categories?
Only 1 at the first row, 3 and 5 in the second one and 7,1,6 at the last one?
Thanks!
You can try this:
...
GROUP_CONCAT(DISTINCT categories_food_shops.id_category) as categories,
....
This should work for pictures also.
Here is the documentation with DISTINCT usage example.