Creating is_sold MySQL field based on index existence in another table - mysql

I have a simple set of products with serial numbers. If a product is sold, its serial number gets placed in sold_items while all items that ever existed remain in all_items.
Some sample data:
all_items
id | serial_number | product_name | product_price
1 | XXXXXXXXXXX01 | Laptop | 199
2 | XXXXXXXXXXX02 | Tablet | 99
sold_items
id | serial_number | sold_time
1 | XXXXXXXXXXX02 | [TIMESTAMP]
I'm trying to do something like this with my query:
SELECT
all_items.* ,
(JOIN sold_items ON sold_items.serial_number = all_items.serial_number) AS is_sold
FROM all_items
In this case, the expected output would be
[
0=>[
'id'=>1,
'serial_number'=>XXXXXXXXXXX01,
'product_name'=>Laptop,
'product_price'=>199,
'is_sold'=>0
],
1=>[
'id'=>1,
'serial_number'=>XXXXXXXXXXX02,
'product_name'=>Tablet,
'product_price'=>99,
'is_sold'=>1
]
]
Is this easily achievable with a single query?

Use a LEFT JOIN and test whether the ID in the other table is not null.
SELECT a.*, s.id IS NOT NULL AS is_sold
FROM all_items AS a
LEFT JOIN sold_items AS s ON a.serial_number = s.serial_number

Related

How to join three tables - MySQL

Read it before marking it as a duplicate question.
There are three tables - invoice_order, invoice_order_item, and stock
and here is the structure of it
invoice_order
order_id | user_id | customer_name | order_date | order_number
1 | 1 | xyz | y-m-d | 0000001
invoice_order_item
Here order_id is a foreign key and order_item_name storing stock_id
order_item_id | order_id | order_item_name | order_item_quantity | order_item_price
1 | 1 | 2 | 5 | 1000
Stock
stock_id | item_type | item_name
2 | goods | Mobile
I'm trying to join these three tables. This is what I achieved till now
$query = "select * from invoice_order inner join invoice_order_item on invoice_order.order_id = invoice_order_item.order_id inner join stock on stock_id = invoice_order_item.order_item_name where invoice_order.order_id = {$order_id}";
The above query doesn't return the order_item_name. It outputs the stock_id instead of the item name. I need to output the name of the item instead of the stock id.
and I know code is vulnerable to SQL Injection. You can ignore it, this is only for testing purpose
SELECT
Stock.item_name
FROM invoice_order
JOIN invoice_order_item
ON invoice_order.order_id = invoice_order_item.order_id
JOIN Stock
ON invoice_order_item.order_item_name = Stock.stock_id
See ref

Update the mysql column with merge of two ROW values

I have a table user_brand in my database, which stores information about brand which user buy. Assuming that my table has no dependency on another table, table looks like this:
User_ID | Brand | meta_key
---------+--------------+-------------
1 | Killer | Name
1 | Lewis | Name
1 | Pepe | Name
1 | Cloth | Product
2 | Samsung | Name
2 | Motorolla | Name
2 | CellPhone | Product
3 | Acer | Name
3 | Laptop | Product
Now I want my brand column to update so that the output will be like below, and if meta_key is duplicated it will be fine for me, I have not much concern about that.
User_ID | Brand
---------+-----------------------
1 | Killer, Lewis, Pepe
2 | Samasung, Motorolla
3 | Nisaan
I achieved to get the output in select query and i.e
SELECT User_ID, GROUP_CONCAT(Brand)
FROM user_brand
WHERE meta_key = "Name"
GROUP BY User_Id
But tried so many way to update that particular column but failed.
Some tried query is:
UPDATE user_brand
SET Brand = (SELECT max
FROM
(SELECT GROUP_CONCAT(Brand) AS max
FROM user_brand
WHERE meta_key = "Name"
GROUP BY User_id) AS t)
UPDATE user_brand AS t1
JOIN
(SELECT
GROUP_CONCAT(brand) AS max
FROM user_brand
WHERE meta_key = "Name"
GROUP BY User_id) AS t2
SET t1.Brand = t2.max
Any help, it will be highly appreciated.
Try below - You've to add ON Clause
UPDATE user_brand t1
JOIN
(SELECT User_id,GROUP_CONCAT(brand) as max FROM user_brand where meta_key = "Name"
group by User_id
) AS t2 on t1.user_id=t2.user_id
SET t1.Brand = t2.max

Match only if all elements of a column are in another table

I've made a little database in SQL that as 2 tables Product (Name, Ingredient and Available (Ingredient):
| Product | Available |
| Name | Ingredient | Ingredient |
| 1 | a | a |
| 1 | b | c |
| 2 | a |
| 2 | c |
I want the name of a product only if ALL its ingredients are inside the Available table.
For the previous example, the result should be: Product "2"
and not Product "1", because I don't have the ingredient "b" in the Available table.
Thanks for the help
You can try with left join (to figure out which Products don't have necessary Ingredients) and group by + having to filter Products that have at least one missing Ingredient:
select p.Name
from Products p
left join Available a on a.Ingredient = p.Ingredient
group by p.Name
having sum(a.Ingredient is null) = 0
You can try something like this also:
WITH TEMP_PRODUCTS AS
(
SELECT NAME, COUNT(1) AS NUMBER_OF_INGREDIENTS
FROM PRODUCT
GROUP BY PRODUCT
)
SELECT PRD.NAME, COUNT(1) AS NUMBER_OF_AVAILABLE_INGREDIENTS
FROM PRODUCT PRD
JOIN TEMP_PRODUCTS TMP ON PRD.NAME = TMP.NAME
WHERE EXISTS (SELECT 1
FROM INGREDIENT ING
WHERE ING.INGREDIENT = PRD.INGREDIENT)
GROUP BY PRD.NAME
HAVING COUNT(1) = TMP.NUMBER_OF_INGREDIENTS;

Select from 2 table SQL WHERE Clause by ID

I have 2 tables in MySQL, in both of these tables I have merchant_id, merchant, branch and some another fields, the name of one table is merchant and another table is product.
tbl_merchant :
id | merchant_id | merchant_name | branch | ...
------+---------------+--------------------+----------------+
1 | 1001 | McDonalds | branch 1 mcd | ...
2 | 2002 | KFC | branch 1 kfc | ...
tbl_product :
id | product_id | product_name | price | merchant_id
------+---------------+-----------------+---------+-------------
1 | 100101 | Chicken | 10 | 1001
2 | 100102 | Potato | 5 | 1001
3 | 100101 | Burger | 10 | 2002
4 | 100102 | Fish Fillet | 10 | 2002
I want to know how can to show merchant_name, branch from both tables using SQL WHERE Clause by product_id = 100101 and merchant_id = 1001 ?
Like this :
Result :
id | merchant | branch | product_name | price
------+-------------+----------------+---------------+-------
1 | McDonalds | branch 1 mcd | Chicken | 10
Thank You
First, I'll show you the query, then I'll explain each part line by line to help you understand:
SELECT
merchant_name, branch
FROM
tbl_merchant INNER JOIN tbl_product ON (tbl_product.merchant_id = tbl_merchant.merchant_id)
WHERE
product_id = 100101 AND merchant_id = 1001
Alright, so if we look at the first part following the select, it should be clear that the two columns that will be printed out are merchant_name and branch. Based on your output, you can print out any field from either table just by adding its name to the list. If the field has the same name in both tables, then you need to qualify it like this:
SELECT
tbl_merchant.id, tbl_product.id
FROM
tbl_merchant INNER JOIN tbl_product USING(merchant_id)
The tricky part of this query is the line that joins the two tables together. Essentially what you have as of now is two tables that are linked together by a merchant id, which makes sense because 1 merchant can have many products (i.e. a 1 to many relationship). I'm assuming that the merchant ID is unique. The join then pairs together all the rows that have the same merchant_id (which is unique in one of the tables and therefore guaranteed to be correct). More specially you can think of it as a qualified cross product where each tuple from tbl_product is joined with each tuple from tbl_merchant and then qualified based on the condition tbl_product.merchant_id = tbl_merchant.merchant_id.
The last part of the query (WHERE clause) simply eliminates rows based on the conditions provided.
The query for this is:
SELECT merchant_name, branch
FROM
tbl_merchant
INNER JOIN
tbl_product
ON (tbl_product.merchant_id = tbl_merchant.merchant_id)
WHERE
product_id = 100101 AND merchant_id = 1001
SELECT merchants.id, merchants.merchant_id, merchants.branch, products.product_name, products.price
FROM merchants
INNER JOIN products ON products.merchant_id = merchants.merchant_id
WHERE merchants.merchant_id = 1001 AND products.product_id = 100101
you can use JOIN to solve this type of query
there are some good article to learn more about JOIN with visula explanation::
1) http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ "A Visual Explanation of SQL Joins"
2) http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins "Visual Representation of SQL Joins"
Try this:
select * from marchant join product on marchant.id=product.merchant_id where merchant_id = 1001
This statement will join both tables together where the primary key form merchant is equals the merchant_id in product.

Mysql cross table join

hi i have 2 mysql table as follow:
items
id item_name user_id
1 test1 1
2 test2 1
3 test3 1
4 test4 1
project
id user_id items
1 1 1,3
2 1 2,4
how can write a join query that can return each items in a project?
project1 =>
item1=>
[id1] =>
[name1] =>
item3=>
[id3] =>
[name3] =>
Thanks.
UPDATE
item tbl
project tbl
First of all don't store strings of delimited values in your db. You're limiting your self with the means to normally maintain and query data. Normalize your data (in this case by introducing project_items table with project_id and item_id columns). It'll pay off big time in a long run.
In the mean time you can use FIND_IN_SET() to join your tables
SELECT p.id project_id, p.user_id, i.id item_id, i.item_name
FROM project p LEFT JOIN items i
ON FIND_IN_SET(i.id, p.items) > 0
AND p.user_id = i.user_id
ORDER BY p.id, i.id
Output:
| PROJECT_ID | USER_ID | ITEM_ID | ITEM_NAME |
----------------------------------------------
| 1 | 1 | 1 | test1 |
| 1 | 1 | 3 | test3 |
| 2 | 1 | 2 | test2 |
| 2 | 1 | 4 | test4 |
Here is SQLFiddle demo
UPDATE: Values of items should not contain spaces. Either remove them or use REPLACE() like this
ON FIND_IN_SET(i.id, REPLACE(p.items, ' ', '')) > 0
Here is SQLFiddle demo
Your approach is not good. You have to create a relational table between items and projects. Including the list of values ​​followed by commas in a record is not a good idea.
You should create an additional table relational called: project_items
and you can use the following sentence to retrieve the items from a project
select project_items.id_project, items.item_name, items.user_id
from project_items
left join items on project_items.id_item = items.id
That's a better approach