Update query with text extraction - ms-access

I have 2 tables:
T1: ProductID / Product (Ex: 1 = PB 2500MM / 2 = PB 2600MM ...)
T2: PriceID / ProductID / Price
So far, so good.
What i am trying to do is to update the price of all the "similar" products (PB products in the example without taking into account the length of the PB)
Thanks for your time!
Matt

That could be:
Update T2
Set Price = 5.5
Where ProductID In
(Select ProductID From T1
Where Product Like "PB *")

You can use an UPDATE query with a LIKE operator
UPDATE T2
INNER JOIN T1 ON T2.ProductID = T1.ProductID
SET Price = 1.05 * Price
WHERE T1.Product LIKE 'PB*'
Note: other databases like Oracle often do not support this way of joining tables in an UPDATE command.

Related

Obtaining only certain values (sql)

I have 3 different tables: table1, table2, table3
Table 1 contains all the different orders that were purchased
Table 2 contains the detail of every order (i mean, it contains a
column called ORDER_DETAIL and the number represent an item of
that order -a unique value)
Table 3 contains the workflow.. some numbers that were inside
ORDER_DETAIL from Table 2 will appear here because this item must be
approved to be delivered
I want to obtain all the different orders whose items did not appear in Table 3.
This picture explains everything:
This is my SQLFIDDLE: http://sqlfiddle.com/#!9/5bfc22/2
I did this query but i am not getting what i want:
select * from table1 kio
inner join table2 jio on kio.ORDER_NUMBER = jio.ORDER_NUMBER
where jio.CANCELLED = 0
and not exists (select 1 from table3 gio where jio.ORDER_DETAIL = gio.ORDER_DETAIL)
Also, how can i obtain those orders whose ORDER_DETAILs only appear on TABLE 2 AND those orders whose order_details appear in table 3 with PROCESSED = 1 and APPROVED = 1? All in the same query.
You can use aggregation: join table1 with table2, then left join table3, aggregate by order_number and filter on groups that have no match in table3.
select t1.id, t1.order_number
from table1 t1
inner join table2 t2 on t2.order_number = t1.order_number
left join table3 t3 on t3.order_detail = t2.order_detail
group by t1.id, t1.order_number
having count(t3.order_detail) = 0
In your DB Fiddle, this produces:
id order_number
3 46646
Also, how can i obtain those orders whose ORDER_DETAILs only appear on TABLE 2 AND those orders whose order_details appear in table 3 with PROCESSED = 1 and APPROVED = 1? All in the same query
For this, you can just add another pair of conditions in the having clause:
having
count(t3.order_detail) = 0
or (max(t3.processed) = 1 and max(t3.approved) = 1)
Yields:
id order_number
1 78945
3 46646
I want to obtain all the different orders whose items did not appear in Table 3.
This seems like a reasonable interpretation of the question, although you add more questions later.
If so, then you don't seem to need table1:
select t2.order_number
from table2 t2 left join
table3 t3
on t2.ORDER_DETAIL = t3.ORDER_DETAIL
group by t2.order_number
having count(t3.ORDER_DETAIL) = 0;

Write a SQL query to select all products with pagination

Two tables are given. In one table we have following columns
productid
product_title
priority
categoryid
and in second table we have
id
productid
color
selling_price
stock
Write a SQL query to select all products in category id 306 with pagination
Lets say Result set would have 1000+ such products so we want to paginate the
results. In a single request only 100 result need to be return
This seems to be a text-book problem.
Still trying to answer. The problem statement is not a straight problem. Here is my answer:
Select Top 100 * from table_2 where productid in (Select (productid, product_title, priority) from table_2 where (categoryid = 306));
For paging please read the link :
MySQL Data - Best way to implement paging?
but your query is:
Select
T1.productid
,T1.product_title
,T1.priority
,T1.categoryid
,T2.id
-- ,T2.productid
,T2.color
,T2.selling_price
,T2.stock
from First_Table T1
inner join to Second_Table T2 on T1.productid=T2.productid
where T1.categoryid=306

SQL Join 2 tables with "ON" on max value in second table

I have 2 tables one with inventory and other with prices list on different dates. I need to update table 1 with price on a particular date which may not be available in table 2 so i need to lookback on last available price. How can I do this. Following are my tables:
Table1
SrNo Commodity Date Price
1 Car 20-Aug-2015 <115>
2 Cycle 20-Aug-2015 <78>
Table2
SrNo Commodity Price Date
1 Car 100 1-Jan-2015
2 Car 120 1-Jun-2015
3 Car 115 20-Aug-2015
4 Cycle 80 10-May-2015
5 Cycle 78 10-Jun-2015
I tried using an inner join but I could get it for Car since it has an entry on 20-Aug-2015. I want cycle to be shown as 78 as it was the last available price.
Can someone suggest me how to do this.
Thanks,
Swati
Next code will work on T-SQL - try so
update t1 set
t1.Price = t2.Price
from Table1 as t1
outer apply (
select top 1
t2.Price
from Table2 as t2
where t2.SrNo = t1.SrNo
order by t2.Date desc
) t2
Try this:
UPDATE a
SET a.Price = b.Price
FROM Table1 a
INNER JOIN Table2 b ON a.Commodity = b.Commodity
WHERE b.[Date] = (SELECT MAX([Date])
FROM Table2 c
WHERE b.Commodity = c.Commodity
AND c.[Date] <= a.[Date]
GROUP BY c.Commodity)
For MySql
UPDATE Table1
JOIN ( SELECT Commodity,Price
FROM Table2 JOIN (SELECT Table2.Commodity,MAX(DATE) As LastDate
FROM Table2
GROUP BY Commodity ) AS Tmp1
ON Table2.Date = Tmp1.LastDate
) AS Tmp2
ON Tmp2.Commodity = Table1.Commodity
SET Table1 .Price = Tmp2.Price
MAX(DATE) is calculated in inner query Tmp1 to get last availaible price of commodity
SQLFiddle Demo

Deleting duplicates with a where clause

Hey so I have one set of data with the structure:
id product_number product_type
1 1001 car
2 1002 house
But the data has some duplicates where:
id product_number product_type
1 1001 car
2 1001 house
I need to delete the duplicates but only the value which is = house.
In my mind the query should be like:
DELETE *
FROM table
WHERE product_number is duplicate AND product_type = house
Thanks
In MySQL, you can do what you want with a join:
delete t
from table t join
(select product, count(*) as cnt
from table
group by product
) tt
on tt.product = t.product
where tt.cnt > 1 and t.product_type = 'house';
You have multiple methods:
1.
You can use this query:
INSERT INTO new_table (SELECT DISTINCT * FROM old_table WHERE product_type = house)
2.
You can alter your table and change your product number column to add an index to it
ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (product_number);
So the duplicated row with the same product number will be dropped automatically, and for more see this link here
3.
You can try this query to:
DELETE n1 FROM table_product p1, table_product p2 WHERE p1.product_number = p2.product_number AND p1.product_type= house
You can use an UPDATE query with a join like this:
delete t1.*
from
t t1 INNER JOIN t t2
ON t1.product_number = t2.product_number
AND t1.product_type='house' AND (
t2.product_type<>'house'
OR t1.id>t2.id
)
DELETE *
FROM table
WHERE id not in
(select max(id) from table
group by product_number)
AND product_type = house

How to fetch data from two tables in sql

I have two tables:
This is table1:
product_id|product_desc|product_name|product_s_desc
This is table2:
product_price_id|product_id|product_price
Now I want to fetch data from these tables. product_id is same in both tables.
I want to fetch
product_s_desc
product_desc
product_name AND product_price from other table.
Please help me do this.
I'm assuming you have a field named product_price in your second table (you didn't list it):
SELECT t1.product_s_desc, t1.product_desc, t1.product_name, t2.product_price
FROM table1 t1
INNER JOIN table2 t2 ON t2.product_id = t1.product_id
You should check out the MySQL manual regarding JOINS, as this is a very basic part of writing SQL queries. You might also consider adding an index on table2 for the product_id field to make the query run faster.
Select * from table1 join table2 on table1.productid = table2.productid
SELECT t1.*,t2.product_price
FROM table1 t1,table2 t2
WHERE t1.product_id=t2.product_id
$sql = "SELECT Student.First_Name,Student.Last_name,Student.Mobile_No,Student.Email,Student.Institue,Student.DOB,Student.Gender
Address.Address_Line1,Address.City,Address.State,Address.Country,Address.Zip_code
FROM Student INNER JOIN Address
ON Student.Id=Address.Id;";