Making a custom table from one table - mysql

My table is called Storage_Tot which has date, tagid, value components.
Below are the two tables (Table A and Table B) I created using SQL query. I essentially ran same query with different tagid with the same table 'Storage_Tot). I would like to combine table A and B and also add a 3rd column which is the sum of the values of Table A and Table B (Table C).
Table A (Left table) Table B (Right Table)
enter image description here
Here is the sample query that I wrote for the two tables.
enter image description here
This is how I would like my final table to look like. As a first step I was trying to just combine the the values of Table A and Table B and after that focus on the arithmetic aspect.
enter image description here
Here is the SQL query when combing the two tables but it does not work
enter image description here
_______________________________________ APPEND
Since I couldn't post under the response question, After implementing your suggestion here is the error that I get.
enter image description here
This is how the query looks as it sits and it appears that its not like where Storage_Tot.TagID = 106
enter image description here

I believe what you are asking for is (1) a JOIN and (2) a calculated column.
Here's the general idea for a JOIN:
SELECT
tblA.date, tblA.value, tblB.value
FROM
(SELECT * FROM Storage_Tot WHERE tagid = 'TABLEA_TAGID') tblA
INNER JOIN
(SELECT * FROM Storage_Tot WHERE tagid = 'TABLEB_TAGID') tblB
ON (tblA.date = tblB.date);
Some of that is guesswork, because you didn't provide complete details in your question. But hopefully you can see what's going on: the sub-queries in a way 'create' what you are calling the two tables shown in your first image.
Now, to add a total, just add a calculated column to the above query:
SELECT
tblA.date, tblA.value, tblB.value, tblA.value + tblB.value AS total_value
I have not verified any of this in an instance of MySQL, but I do believe I have the syntax correct for MySQL. You might need to make small adjustments for typos.. I have verified the above syntax via SQL Fiddle. Of course you need to fill in what TABLEA_TAGID and TABLEB_TAGID really are.
There are also lots of tutorials and references for using JOINs on-line. There are various kinds of JOINs, and I'm only showing one kind here.

#landru27: Thank you so much for your help. I don't know how I missed that from/where syntax. Anyway, I took your direction and had to make minor changes and the code executed.
Below is the final code that I shall be using for my query.
enter image description here

Related

mysql database column issue

Create a list of all players and the fees they pay. The football league is looking to increase fees by 15%. Show that column in your list. When complete your output should look like the following. Note: your spacing may be a little different. This output is formatted to be smaller.
[1]: https://i.stack.imgur.com/RmN6D.png - Question with the format required
[enter image description here][1]
[2]: https://i.stack.imgur.com/iJNVS.png - Picture of the table that is going to be used. The table name is 'playerrec'
I know that you will have to use an alias for the column Fee it would be renamed to Increased Fee, but where I am getting stuck at is that I don't know what to do for the New Fee since it isn't a column that is existing. I've already thought about creating column, but I looked at my teachers lectures and it seems we won't be getting into that for a bit.
You can operate on a column in your SELECT clause using arithmetic operators.
If you were looking to get the base fees off the playerrec table, you could do so by simply selecting the column
SELECT pr.Fee
FROM playerrec AS pr
If you were looking to get, say, double the normal fee, you could do so like this:
SELECT (2 * pr.Fee) AS DoubledFee
FROM playerrec AS pr
Here you're computing a new column and aliasing it as 'DoubledFee'. This does not change the underlying data nor add a column to the table. If you want a comparison, you could select both together
SELECT Fee, (2 * pr.Fee) AS DoubledFee
...
For more, see https://learn.microsoft.com/en-us/sql/t-sql/language-elements/multiply-transact-sql?view=sql-server-ver15
As a bonus, the microsoft example includes a 15% change

In MySQL is there a way to chose one column over another when doing SELECT *?

Ok, for a moment, throw out of your mind "good database design". Let's say I have two tables, and they have some of the same columns.
item
-------
id
title
color
and
item_detail
-------
id
weight
color
In a good normal query, you'd choose the columns you want within the query, like so:
SELECT item.title, item_detail.color, item_detail.weight ...
But what if you are stuck with a query that was built with star/all:
SELECT * ...
In this case you would get two color columns pulled back in your results, one for each table. Is there a way in MySQL to chose one color column over the other, so only one shows up in the results, without a full rewrite of the statement? So that I could say that the table item_detail takes priority?
Probably not but I thought I'd ask.
Err. No there is not.
But define "without a full rewrite of the statement". As far as I can see you'd just need to rewrite the select * portion of the query.
If you cannot touch the statement at all, then you are free to ignore the column in your application (the order of the columns does not change between calls)... or you could create a view...
It's hard to know which constraints you are dealing with when you say "But what if you are stuck with a query".

Mysql: query not giving accurate result with IN clause and inner query

I'm trying to get zip codes from zip_id's which are internally stored in companies service table below screens will give you clear idea
I have wrote this query
companies service table
Please suggest me your valuable views . Thanks in advance.
As already mentioned your database scheme is not very well designed, it violates even 1st normal form. You'd need another table where you'd store serv_area_id and zip_code (with possibly multiple rows for a signle serv_area_id) and search within this table and eventually join your original table.
Nevertheless, in order to get the result you describe you cannot use the IN operator as it operates on a value and multiple values in a form of table (either explicit via nested SELECT or enumeration literal (val1, ..., valN)). I would try some string matching as illustrated below. However, consider it rather an ugly hack than correct solution(!)
SELECT zip FROM cities_extended WHERE (
SELECT GROUP_CONCAT(',', serv_are_zipcodes)
FROM company_service_areas WHERE ...
) LIKE concat('%(', id, ')%')

Read-only after inner join (MySQL)

In MySQL workbench (Mac OS), I wanted to join two tables so that I can update the second one. The code I put in was as follows
select f.company, f.remarks, c.pic
from feedback f, customers c
where f.col = c.col
order by f.company;
The output is a read only table, which prevented me from updating table "customers" based on the f.remarks column.
Your advice/suggestion is appreciated. Thank you.
By hovering above the "Read Only" icon, I got the following message:
"Statement must be a SELECT from a single table with a primary key for its results to be editable".
After some research based on the advice given by fellow coders, here are some points to note:
In MySQL workbench, one cannot edit the results obtained from any JOINs because it's not from a single table;
In using SELECT from a single table, the primary key must be included in order for the result to be editable.
Thank you to everyone who contributed to the question. I appreciate it.
The problem is because, as you mentioned, SELECT only returns a "read only" result set.
So basically you cant use MySQL workbench to update a field in a read only result set that is returned when using a JOIN statement.
from what i understand you want to update table "customers" based on a query.
maybe this post will help you:
update table based on subquery of table

How inefficient would this query be and also table structure?

I have two tables. One table stores course_updates, which basically has a new row pushed to it everytime someone adds or drops a course. I also have another table follower_updates that has a record pushed to it whenever someone follows someone. I want to be able to get the information for the logged in user, but I'm unsure how I should detect which table the information is coming from being that I want to display the information based upon which table it is from. Should I make a new column that update_type, or should I have a different method?
I'm also going to show what I'm thinking relatively in terms of sql. It won't be perfect because I haven't tested it yet. This is just a sample. I didn't want to bring in my current query because just the course_updates already has three inner joins and an outerjoin, so I tried to streamline the content for this question. thanks!
SELECT * FROM course_updates WHERE (establishes connection
enter code herebetween user and courses and followers)
UNION SELECT * FROM follower_updates WHERE followee.id = currentUser.id etc.
Don't use a UNION. Use two separate queries instead.