Comparing fields of one table to other fields of another table - php

I have a function called add_item() which actually inserts values in a field item_name of temporary table called temp having fields temp_id and item_name. I have another table calleed item which consists of fields item_id, item_name, price. I have another table called quotation which consists of fields q_id, item_id,item_name,price.
Now I cant figure out how do i compare the item_name from temp to the field item_name from item. And then, insert the values of item in quotation table. Can anyone guide me please?
For example i have the following records in temp
temp_id item_name
1 kit
2 drill
3 tester
and i have the following records in item:
id item_name price
1 drill A 100
2 drill B 354
3 drill C 743
4 tester 643
5 cccc 643
What I want to do is compare the item_name from temp table with that of table item.
after comparing i should insert these values in table quotation
quotation_id searched_name item_name price
1 kit not found null
2 drill drill A 100
3 drill drill B 354
4 drill drill C 743
5 tester tester 643
Can someone please guide me where to start?Thanks

//select query for getting data from both tables
$get_data=mysql_query("SELECT *,temp.item_name AS searched, item.item_name AS response
FROM temp JOIN item ON temp.item_name = item.item_name");
while($datas=mysql_fetch_array($get_data))
{
//inserting into quotation table
mysql_query("INSERT INTO quotation SET
searched_name='".$datas['searched']."',
item_name='".$datas['response']."',
price='".$datas['price']."' ");
}

If you mean how to refer to fields in multiple tables/databases in a single query, you use the '.' notation:
SELECT table1.id, table2.value, table3.discount etc...
with the appropriate tables specified in your FROM and JOIN clauses. This can be extended to refer to fields in other databases entirely:
SELECT table1.id, table2.value
FROM database1.table1, database2.table2
update:
INSERT INTO quotations
SELECT item.item_name AS item_name, price
FROM records
LEFT JOIN temp ON item.item_name = temp.item_name;
should do the trick, assuming your quotations table has item_name and price fields. This will pull out any records where the item_Name is the same in both tables, and insert the item_name and price into quotations.

Related

how to get user name based on id separated by comma in single table

user_id user_name user_friend_list
1 dharmendra 2,3,4,5,6,7
2 jitendra 1,3,4,5,6,7
3 xyz 1,2,6,7
4 pqr 1,3,4
now i want to extract user_id & user name based on user_friend_list id 6 i.e
it will return 1,2,3 user_id & user name dharmendra jitendra and xyz.
i simply use splite function of php but it is so complicated please provide me well shortcut method
thanks & regards
You can use FIND_IN_SET()
select user_id, user_name
from your_table
where find_in_set(6, user_friend_list) > 0
But it would actually be better to change your table design. Never store multiple values in one column!
first thing the table is not normalized that's why the problem exist
you must break this table like this or better :)
table 1
user_id
user_name
table 2
user_id
friend_id
table 2 will have some redundant data though which can be removed by adding a third table as a mapping between table1 and table2
now you can have the following query to get the result
select user_id,user_name from table1 as a join table2 as b on a.user_id=b.user_id where b.friend_id=6;
"SELECT user_id,username from your table WHERE user_id IN (2,3,4,5,6,7)";

SQL...updating product prices but keeping previous values

I have a table full of products including price, name, date and ID (PK)
I am in need to update the records when there is a price change (Web scraping script), how would i go about this so that a new row is NOT inserted, rather the price and date are updated.....but the previous values need to remain in the DB....so that when i when i go
"SELECT * FROM items WHERE id ='27'";
Output wanted:
$400 12.4.2013
$314 22.4.2013
$250 12.4.2013
The product will be then displayed with all the updated values since the first price was downloaded, effectively it will be a history of the prices for the item...
the actual results i want to achieve would hopefully be
History for "Product 27" is:
To give more context, when i run my script... ./script.php update
all the new data should be inserted or updated....but old values remain
im not too sure how i should approach this....please provide me with some guidance or assistance
The best way to go about this while keeping a tidy database with easily maintainable and readable data would be to take what #KishorSubedi said in the comment and create a log table.
When you update your price in your Products Table, store the old price, along with its date and ID in the Log Table then when you need to look up all the records for that product you can JOIN the Log Table in your query.
Log Table
| ProductID | Price | Date |
| 27 | $300 | 02.1.2013 |
| 27 | $400 | 03.1.2013 |
This way you can have a nice and neat Products table that is not cluttered with multiples of the same product, and an unobtrusive log table that is easily accessible.
Hope this gives you some guidance on building your site and database.
If you want to keep the old values I would suggest saving them in a seperate table and to use the INSERT statement. You are always adding a new row, so you can not bypass an insert or select into or similar statement.
Table structure:
Items
------------------------------
Id primarykey
Name
Price
------------------------------
Id primarykey autoincrement
ItemId index
AddDate index
Price
Now when you scrape the web you will just insert the new price in the Price table. If you want to select all the prices for an item you can use
SELECT Items.Id
,Items.Name
,Price.Price
,Price.AddDate
FROM Items
LEFT JOIN Price
ON Items.Id=Price.ItemId
WHERE Items.Id='27'
ORDER BY Items.Name ASC, Price.AddDate ASC
And if you just want the latest or current price, you can use
SELECT Items.Id
,Items.Name
,P1.Price
,P1.AddDate
FROM Items
LEFT JOIN Price P1
ON Items.Id=Price.ItemId
LEFT JOIN Price P2
ON P1.ItemId=P2.ItemId
AND P1.Id<P2.Id
WHERE Items.Id='27'
AND P2.Id IS NULL
Add new column for history in 'item' table and each price change append the changed value to this column with a specific format as like date1:price1,date2:price2,....

Show each number separated after comma in php

I have two tables
category
id | category
------
1 | One
2 | Two
etc
second one is
mp_photos
id|name|test|CategoryID
1 |name|test|1,2
So now i need to fetch data from mp_photos row CategoryID (1,2) every categoryid is seppareted by comma and link every number with Category name in first table
Best option would be to restructure your database and create another table with id_mp_photos and id_category and use joins.
If you cannot do it there are two options (both are slow):
use explode() in php
use MySQL function FIND_IN_SET(), for example:
SELECT mp_photos.name, mp_photos.test, category.category
FROM mp_photos JOIN category ON FIND_IN_SET(category.id, mp_photos.CategoryID) > 0

how to update table using data from another table

im using php as my language, and oracle xe for my database.
i have table Order_details that store item_id, quantity.The table currently only hold data for item_id, while the quantity has null value.Lets say the item id is 038
i created another table order_details2 as a temp table that have the same row which is item_id,quantity. This table contains data for both item_id, and quantity which is 038, and 3
the problem is, i dont know how to use data in order_details2.quantity which is 3 to insert it into order_details.quantity using the same references item_id
can any1 please show me, how am i doing this? im very new to programming..
If I understand correctly what you're after, then something like this should work:
UPDATE
(SELECT o1.item_id as id1, o1.quantity as qty1, o2.item_id as id2, o2.quantity as qty2
FROM order_details o1
JOIN order_details2 o2 on o1.item_id = o2.item_id) t
SET t.qty1 = t.qty2
You have to be careful about duplicate item_id values in any of the tables though.

is it possible to transfer table data from one table to another?

is it possible to copy data from a table to another table?.
here is my example. i have two tables, warehouse and showroom
my warehouse table has (product_id and stock_quantity) and my showroom table has (product_id(fk from warehouse), stock_transferred )..
for example in warehouse table
product_id stock_quantity
1 10
2 20
how can I transfer product_id(1)with stock_quantity(5) in showroom table and still retain the data in warehouse table?
so after transferring data my warehouse table becomes these:
product_id stock_quantity
1 5
2 20
and my showroom table becomes these:
product_id stock_transferred
1 5
how can i do these in a php form?for example i have a text input in which the user can specify how many stocks he will transfer to showroom table.
sorry I cant explain well Im not good in english.
BEGIN TRAN
INSERT INTO showroom
SELECT product_id, #ValueToBeReduced FROM warehouse
WHERE product_id = 1
-- error handling
UPDATE warehouse
SET stock_quantity = stock_quantity - #ValueToBeReduced
INNER JOIN showroom
ON warehouse.product_id = showroom.product_id
AND showroom.product_id = 1
-- error handling
COMMIT TRAN
You can use insert into(select...)

Categories