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
Related
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.
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.
I have a table called lis_pendens and a table called docket_entries. Each lis_pendens record has many docket_entries, where the foreign key of docket_entries is case_id. If the description field of docket_entries contains the text 'writ' or 'title' or 'sale' or 'dismissal', then I do not want to return the associated lis_penden record in the result set. Otherwise, if none of the docket_entries belonging to a lis_pendnes record contain these keywords, then I want to return the lis_pendens record in the result set.
Here is the query I have created:
SELECT lis_pendens.id as id
FROM lis_pendens
INNER JOIN docket_entries
ON docket_entries.case_id = lis_pendens.id
WHERE no_foreclosure_sale_created_at IS NULL
AND NOT EXISTS (
SELECT 1 FROM docket_entries AS e
WHERE e.case_id = lis_pendens.id
AND LOWER(e.description) REGEXP 'writ|title|sale|dismissal'
) GROUP BY id
I thought the query was good but it's producing EVERY lis_pendens record in the database, even though some of those lis_pendens records have associating docket_entries whose description field contains this:
Notice of Voluntary Dismissal
Notice of Dismissal & Discharge of Lis Pendens
Certificate of Title
...
Obviously, something is wrong with the query. What am I doing wrong?
You could use MAX() to perform a group-wise NAND operation:
SELECT p.id
FROM lis_pendens p
INNER JOIN docket_entries e ON e.case_id = p.id
GROUP BY p.id
HAVING MAX(LOWER(e.description) REGEXP 'writ|title|sale|dismissal') = 0;
I have this query made in codeigniter using active record
$this->db->select('products_main.*, images_main.*, items_main.*, product_tags.*');
$this->db->from('products_main');
$this->db->join('images_main','products_main.product_tag = images_main.img_tag AND products_main.product_id = images_main.pro_img_id');
$this->db->join('items_main','products_main.product_id = items_main.pro_items_id');
$this->db->join('product_tags','products_main.product_id = product_tags.pro_tag_id');
$this->db->where('products_main.product_id',$p_id);
$this->db->where('products_main.status','active');
$this->db->group_by('products_main.product_id');
$query = $this->db->get();
My goal is to:
Grab one product from products_main that matches the input from $p_id
Grab all the photos for that one product from the images_main table
Grab all the items that have the product_id of the product I have found
Grab the row in the product_tags table that match the product_id of the product i have found.
I started a test fiddle here but have not filled out the query part yet as I do not know what to put. fiddle
You don't have any problem within your query, but as you group by product id, you may only get one result, but you can group by image_id instead, i've created the same in my localhost with the database you've created a fiddle and tried your solution, your only problem i see was as you don't join on left, the query can't find any exact matches, so try to join on left, also i've changed your query a little bit to prevent the database error with the given structure in your fiddle, so change it if you have them like that in your database ( commented them ) :
$this->db->select('p.*, im.*, i.*, pt.*');
$this->db->from('products_main as p');
// img_tag -> image_tag
$this->db->join('images_main as im',
'p.product_tag = im.image_tag
AND p.product_id = im.product_id','left'); // pro_img_id -> product_id
$this->db->join('items_main as i',
'p.product_id = i.product_id','left'); // pro_items_id -> product_id
$this->db->join('product_tags as pt',
'p.product_id = pt.product_id','left'); // pro_tag_id -> product_id
$this->db->where('p.product_id',"23"); // i have given example id as 23
$this->db->where('p.status','active');
$query = $this->db->get();
return $query->result_array();
I am trying to build a rather complex view in MySQL and want to do a conditional, but it sems always to fail.
My view (simplified) is
Select entry AS Entry, ,(select count(`poitems`.`entry`) AS `count(poitems.entry)` from `poitems` where (`poitems`.`PurchaseOrder` = `purchaseorder`.`entry`)) AS `TotalEntries`, from purchase orders
this is OK but what I am trying to do is add something like
if ((select count(`poitems`.`entry`) = 0),'query.png',NULL) AS Queryflag
or just test the value of TotalEntries.
Help appreciated! Thanks!
I'm not 100% sure on the names of the columns in purchaseorder or poitems tables but the following should get you headed in the right direction:
select t.Entry,
case when t.TotalEntries > 0 then 'query.png' else null end as Queryflag
from
(
select po.entry as Entry,
count(*) as TotalEntries
from purchaseorder po
left outer join poitems poi on poi.purchaseorder = po.entry
group by po.entry
) t;