Mysql concat and search on multiple tables - mysql

I have 2 tables, products and categories defined as this:
Table: categories - category_id, category
Table: products - product_id, category_id, item_no, description
I'm trying to concat the description and category into a single searchable field. When I do the following it works, but is clunky code:
SELECT *, concat(description, ' ',category) as searchable
FROM products, categories
where concat(description, ' ',category) like '%flowers%'
and concat(description, ' ',category) like '%rainbow%'
and products.category_id=categories.category_id order by products.category_id
So I'm trying to write a sub-query like this:
SELECT *
FROM products, categories,
(SELECT concat(description, ' ',category) as searchable
FROM products, categories
where products.category_id=categories.category_id
group by product_id) as sub
where searchable like '%flowers%' and searchable like '%rainbow%'
and products.category_id=categories.category_id order by products.category_id;
This query returns multiple incorrect results for every product_id, and when I try to group by product_id, every result shows the improper searchable field.
Any help would be appreciated.

Sometimes the key is to simplify. The following will find products that have descriptions or categories matching '%rainbow%'.
By breaking up the query into two LIKE clauses, we're also improving performance by eliminating a heavy CONCAT operation.
SELECT p.*
FROM products AS p
LEFT JOIN categories AS c ON p.category_id = c.category_id
WHERE p.description LIKE '%rainbow%'
OR c.category LIKE '%rainbow%'
GROUP BY p.product_id

Related

How to get result from using subquery

I am using MySQL to get some additional information from database. I am trying to do widget bar:
Here is table structure:
My query:
SELECT * FROM `product` WHERE category in (SELECT name FROM category where id='5')
What i want is i need to display category names and products numbers.
But above query gives me empty result set. What am i doing wrong ?
Its only my assumption, since there is little info
This query gets a list of categories and for each adds a count of products having id of that category
select c.id, c.name, count(p.id) count
from category c
left join
product p
on p.category = c.id
group by c.id
You will get rows with category id, category name, product count

Proper SQL Query for Displaying the name instead of ID Reportico

I have 3 tables, Tickets, Category, and Sub Category. In Yii2 Reportico in order to generate reports you need to configure the SQL Query.
SELECT id, time_start, time_end, details, room_no, employee_id, category_id, sub_cat_id FROM tickets
This is my SQL Query for getting data in tickets table, instead of displaying the category_id and sub_cat_id. I want to display the category_name and sub_category, what is the proper sql syntax?
You can try something like this and see if you get the category name:
SELECT t.id, t.time_start, t.time_end, t.details, t.room_no, t.employee_id,
c.category_name , s.sub_category
FROM tickets as t inner join Category as c on t.category_id=c.id inner join
sub_cat as s on t.sub_cat_id=s.id and s.category_id=c.id
I'm not aware of field names you have in Category and Sub Category tables, but if it works for Category, try adding Sub Category table in a similar manner, or post the field list of both tables.

Joining two tables and selecting from the result

I am trying out SQL and having trouble figuring out making queries when it comes to combining info from multi tables. Been using w3school but they don't seem to have similar reference to the question below. Was thinking of joining them as my codes below but still that doesn't answer the question. Appreciate any advice. Thanks.
Question:
Products(itemID, description, quantity, supplierID)
Supplier(supplierID, name, address)
A product can be supplied by more than one supplier. Write the SQL to
list the quantity of each product by each supplier.
SELECT Products.quanity, Supplier.name
FROM Products INNER JOIN Supplier
ON Products.supplierID = Supplier.supplierID;
Assuming that the product table does not have two rows for the same supplier and product, then your query is very close. I would write it as:
SELECT p.description as ProductDescription, s.name as SupllierName, p.Quantity
FROM Products p INNER JOIN
Supplier s
ON p.supplierID = s.supplierID
ORDER BY p.description, p.Quantity desc;
Note the following:
The use of tables aliases (the p and s) make the query more readable and are used for every column reference.
The final result is explicitly order by product, with the largest quantity first.
If there are multiple rows for a given product and supplier, then you will need aggregation.
Looks like you need to use group by here:
select itemId, supplierId, sum(quantity) from Products group by itemId, supplierId;

Custom format mysql query and multiple values in one column

Forgive me if I get some terms wrong, still trying to learn advanced mySql queries. I am trying to export data from a ecommerce platform, and some of the data I need is in lookup tables, the issue is the one lookup table I have can have more than 1 value associated, and the system I need to get the data into requires a specific format for that column.
I am using zen cart as my source if that helps, and below is the query.
Select zp.products_id as id, zpd.products_name as name, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
LEFT JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
What I need is theres a look up table that tells me what categories belong to each product, I know it's going to be another join, but I need it so if more than 1 category belongs to a product to put it in the same column, and join them with a # symbol, and its not the id's of the category, its the path, so I need to look up the category ID to another table to get the path.
So "category1/subcategory#category2" for example..
Thanks for any guidance.
EDIT: I still need to get the results merged as I am getting double the results I want, but I see a record for each category it's in with this..
Select zp.products_id as id, zpd.products_name as name, zcd.categories_name as categories, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
What you are looking for is group_concat(). I think it would be something like this:
Select zp.products_id as id, zpd.products_name as name,
group_concat(zcd.categories_name separator '#') as categories,
products_price_w as cost, products_price as price,
zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
group by zp.products_id
ORDER BY zp.products_id ASC;

MYSQL select results from 3 tables with an array of ids

Ok, so I have 3 mysql tables where I need to extract data from. Anything to do with joins really gets me stuck!
Table 1 = products (productid, name)
Table 2 = category (categoryid, name)
Table 3 = categoryproduct (categoryid, productid) - my join table
I have an array of product ids which I need to get a random selection of products that fall into the same categories as these products.
The idea is that the results of the query will display a section in my cart of similar/related products that the customer may like
So something like
SELECT name etc FROM table1
WHERE table2.categoryid of results of the query = table3.categoryid of current products
ORDER BY RAND()
LIMIT 3
How do I write that??
Assuming you're using PHP, following method will fetch 10 related products from database.
$productids = array(1002,789,999,203,321);
$sql = '
SELECT * FROM
products p JOIN categoryproduct pc
ON p.productid = pc.productid
WHERE pc.categoryid IN(
SELECT DISTINCT(categoryid) FROM
products inner_p JOIN categoryproduct inner_pc
ON inner_p.productid = inner_pc.productid
WHERE inner_p.productid IN('.implode(',',$productids).')
)
ORDER BY RAND()
LIMIT 10';
If i have understood your problem correctly then this query may help. Here instead of subquery you can give comma separated string which contains categoryid of different products selected by the user.
select p.name
from products p,categoryproduct cp
where p.productid=cp.productid
and cp.categorid in(
select categoryid
from cartitems)
order by RAND()