I am new to SQL and am currently doing a project on MSQL 2019.
I have one table with a list of codes mapping a "Loss ID" to a Reason.
Now I have another table that just gives you the number of the Loss ID but not the text version of it.
How can I create a table that will take the number code and automatically change it to the text version of it?
Im not even sure what this type of scripting is called, so even pointers on what to google or look for would be very useful.
Rather than creating another table use a view.
Let's call your tables tbl1 and tbl2. The view would be:
CREATE VIEW my_view AS SELECT tbl2.your_date_column,
tbl2.LossID,
tbl1.reason
FROM tbl1
INNER JOIN tbl2 on tbl1.LossID=tbl2.LossID;
Lear more about VIEWS https://www.mysqltutorial.org/create-sql-views-mysql.aspx
Related
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
Is there a way in Access to link two variables together across separate tables?
Ex. I have one variable in table A that says Consent1 was signed.
Table B will be a summary table showing which consents are needed. In other words, If Consent1 was signed in table A, table B will show Consent1 is NOT NEEDED.
You could build a trigger to update Table B, but if your database is normalized properly there's no need to do that. Just build a query that does the same thing. Join Table A to Table B linked on whatever common field they have (I can't tell you exactly how to do that since you've offered no table structure), and then add a field to the query that looks something like this:
Consent: IIF([Table A].Consent = "Signed", "Not Needed", "Needed")
I have some data in mysql and was wondering whether I can create a new table and then add some type of formula that can count existing tables? For example, I have a table called 'Reviews' which contains 150 rows. Can I have a separate table within mysql that can tell me there are 150 rows without me logging in to see this? Any help appreciated.
You can create a VIEW that contains the count and has other permissions than the table;
CREATE VIEW ReviewCount AS SELECT COUNT(*) FROM Reviews;
This view will have the read access of the defining user, and you can hide the underlying table from the other user, while he can still use the view to get the count.
The thing is, as far as the database is concerned, the other user still has to be logged in. If you want truly anonymous access, you'll have to wrap the database inside your own code, for example a web service.
EDIT: Your comment indicates that what you're looking for is simply a summary table of counts from a number of tables. In that case, you can simply create the view as;
CREATE VIEW test_count AS
SELECT (SELECT COUNT(*) FROM reviews) reviewCount,
(SELECT COUNT(*) FROM records) recordCount,
...
;
An SQLfiddle to test with.
Is it possible to find the name of the table for which a column in a MySQL view belongs?
If a view was constructed as
CREATE VIEW alpha_view AS
SELECT alpha.col1, alpha.col2, beta.col2 FROM alpha
INNER JOIN beta
ON alpha.col1=beta.col1;
then when I edit, I want to only affect alpha.col1 and alpha.col2 as beta.col2 is derived from another table.
I assumed the easiest way of knowing whether it's editable or not comes from knowing if the original table matches the view's FROM clause.
Is this even possible, or should I just make a list of editable columns for my parsing script?
Yes, it is possible. Just use aliasing (via AS keyword) to give different name for result column.
CREATE VIEW alpha_view AS
SELECT alpha.col1 AS a_col1, alpha.col2 AS a_col2, beta.col2 AS b_col2 FROM alpha
INNER JOIN beta
ON alpha.col1=beta.col1;
I am creating a relational database for a friend and I have never used MS Access before. I am trying to make it so that the related values are shown on a table view rather than the numeric representation. For example, instead of
[1][2242], [2][443]
I would like
[1][John Smith], [2][Marilyn Monroe]
(the first value being the primary key and the second being the value linked to another table).
I have read this page about creating relationships between tables but I am still having trouble "viewing" the linked values. Microsoft Access is going to be the only way my friend is going to be viewing these results so that is why it is important to be able to see the correct values.
I have made relational databases before but only with traditional PHP and SQL and so I know what I need to do but I cannot seem to do it.
Any help would be appreciated.
Do not be misled into creating lookup values in tables. Create a form and add comboboxes to display the data from the related table, or use queries.
For example: create form to add records in multiple tables
Or
SELECT Id, Fullname FROM Table1
INNER JOIN Table2
ON Table1.FKID = Table2.ID
You can create the relationships visually in MS Access. You can also build queries in the query design window.