I have two tables cart and cages, I am trying to update the 'remaining' column by checking the item_name for all entries in cart and subtracting the quantity in the corresponding tables.
[cages]
id name total remaining
-----------------------------------
1 Cage1 10 10
2 Cage2 15 15
3 Cage3 10 10
[cart]
id item_name quantity
-----------------------------------
1 Cage1 2
2 Book4 3
There are several tables in the database that would need to be checked upon purchase however, I am not sure what would be the best way of performing these queries without joining all tables. This is the query I have been attempting to use.
UPDATE cages
SET remaining = (remaining - cart.quantity
WHERE cart.item_name = cages.name);
You must join the tables before you can update
UPDATE cages
JOIN cart on cart.item_name = cages.name
SET remaining = (remaining - cart.quantity)
Related
I have two tables and I would like to compare one value to see if it is less than another value.
Considering the two example tables I want to make a SELECT statement that would tell me, given my wallet amount which items I could afford. How do I say:
SELECT product FROM Store WHERE price < amount
The above obviously does not work I have searched everywhere.
Wallet:
name amount
--------------
Mymoney 20
Store:
product | price
-----------------------
Apple | 3
Orange | 4
Steak | 21
As you are working with 2 diferent tables, you should use some kind of join like:
select product from Store inner join Wallet on price <= Mymoney
sqlFiddle:
http://sqlfiddle.com/#!9/b431a2/4
I have searched many other questions, but cant find a usable comparison. I have a SQL table that has a list of all the rooms at our hotel. I want to know the next time each room is going to be occupied. There are two tables I am looking at, one contains the name and details of each room we have, for all intents it is a static table. The other table is a reservations table that shows check in and check out times, and has a column that references which room is being used.
Table Rooms
unique id name
1 Room 3
2 Room 4
3 Suite 1
4 Suite 2
5 Suite 3
Table Reservations
unique id start date room id
1 12/4/16 3
2 12/4/16 4
3 12/6/16 3
4 12/12/16 3
5 12/14/16 2
6 12/20/16 2
This would return only 3 values:
2 12/20/16
3 12/4/16
4 12/4/16
If also possible I would like the make it so that if a reservation is not found a null value is returned, so ultimately, the return value would be
Room Next Occurrence
1 null
2 12/20/16
3 12/4/16
4 12/4/16
5 null
Is there a way of doing this without my current php hack that runs 200 sql queries?
Thanks so much!
You can use left join
select rooms.id, rooms.name, reservations.`start date`
from Rooms
left join reservations on reservations.`room id` = Rooms.id
order by rooms.id
I am designing a mysql database and have come across these relations which will grow in the future.
Suppose Customer is tied to 2 different tables Policies and Options.
Each customer has multiple relationship with policies and likewise with options. Since I am keeping a details and history of the table as well every time I add a relation with customer, I will have to maintain another 2 tables. To calculate the price the customer owes, I will have to go thru customer_policies then customer_options and calculate the total price. Also the number of tables increases as the relationship increases.
If customer has a relation with policies it will have 2 tables -
customer_policies and customer_policies_details.
If customer has one more relation with options, it will add 3 more -
customer_options, and customer_option_history.
Like wise, it will keep on adding 2 more tables if there is one more
relation and the problem grows and grows.
I have tried 2 different options which I have mentioned below. I wanted to know what is the best way to solve this problem so that the table can be maintained as the relation grows.
Option 1:
customer_policies:
CustomerPolicyId CustomerId PolicyId Status
1 1 1 Active
2 1 2 Active
customer_policies_details:
CustomerPolicyDetailsId CustomerPolicyId Price
1 1 10
2 2 20
customer_options:
CustomerOptionId CustomerId OptionId Status
1 1 1 Active
2 1 2 Active
customer_options_details:
CustomerOptionDetailsId CustomerOptionId Price
1 1 10
2 2 20
Option 2:
Create a single table customer_selections and use Type and Id field instead like so:
customer_selections:
CustomerSelctionId CustomerId Type Id Status
1 1 Policy 1 Active
2 1 Policy 2 Active
3 1 Option 1 Active
4 1 Option 2 Active
customer_selection_details:
DetailsId CustomerSelctionId Price
1 1 10
2 2 20
3 3 10
4 4 20
To create a history of this I just have to create a customer_selections_details and keep track of all changes.
There should be better ways to solve this problem.
I'm new to MySQL procedures and triggers and constraints and I'm wondering how I can set one of my columns to be dependent on another column. For example, I have a table of products and weekly sales similar to:
Product_ID Sales Ranking
1 13
2 12
3 543
4 435
5 97
I want my ranking column to be automatically updated whenever a value in the sales is updated or a new value is inserted (this table will only hold at most 50 products), which means product IDs will come and go. How can I set it so that the Ranking is always an x value such that x represents what position it would be it the product id was ordered by sales DESC?
Product ID is NOT an AI column or Primary Index
I have table with colimns
ID|NAME|AGE
1 |name1|40
1 |name2|45
2 |name3|30
2 |name4|39
result i want like this
ID1|NAME1|AGE1|ID2|NAME2|AGE2
1 |name1|40 | 2 |name3|30
1 |name2|45 | 2 |name4|39
there are around 5k rows.
Thanks.
You can get a full product of the tables:
select table.col1,table.col2,table.col3,table2.col1,table2.col2,table2.col3
from table, table2
where table.col1='test' and table2.col2='test1'
The result may have duplicate records from the both tables. But as you don't have any primary keys that's possibly not an issue for you.