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.
Related
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
I'm currently thinking about a database schema in MySQL where I store SELECT queries into a certain table column, just to execute them on-the-fly when getting selected, and having the result passed instead of the actual query.
Would this be possible somehow? Or may this be bad practice? Is it even technically possible to have a result table passed to a single field, at least so I could run the query through PDO to get back a nested result array? Are there any alternatives?
I've read that this may be achieved through stored procedures, and although I grip the concept of those I can't think of how I could use those to achieve that.
You could do this, but what purpose do you have for doing it?
I would suggest using views:
The syntax should be valid when the view is created, unlike storing
the SQL in a field which may have invalid syntax.
It's easier to debug and modify.
For example, let's say one of the queries you want to store is:
SELECT product_category, COUNT(*) AS category_count
FROM product
GROUP BY product_category;
You can create a new "view" object that defines this query:
CREATE VIEW prod_cat_count AS
SELECT product_category, COUNT(*) AS category_count
FROM product
GROUP BY product_category;
Now, the object called "prod_cat_count" is stored in the database. Internally, the database just knows that "prod_cat_count" is equal to the SELECT query we mentioned. When the view is created, the database validates the syntax (checks that all columns exist, checks you haven't forgotten the GROUP BY, for example)
Then, whenever you want to get this data/run this query, you can run this statement (in SQL or in application code, for example):
SELECT product_category, category_count
FROM prod_cat_count;
If you then decide you want to change the way the product categories are counted, you can adjust the view:
SELECT product_category, COUNT(*) AS category_count
FROM product
GROUP BY product_category
ORDER BY product_category;
Hope that helps!
I'm attempting to connect two tables in MySQL so that when selected, a value is pulled from the table.
Table 1
id
mobile_car
Table 2
id
mobile_car
car_domain
When the mobile_car is selected via a dropdown and the user registers this, I would like to be able to have it connect to the car_domain so I can call this later in the application.
For example, if the user selects "Verizon" as their mobile_car, I would like it to link to the car_domain "#vtext.com" so I can connect their phone number with the car_domain. Essentially, I'm sending a text by way of emailing their cell number.
I know there are programs and pre-built options, but I'd like an understanding of how this could be done.
Thanks!
Try joining your two tables together:
SELECT t1.mobile_car, t2.car_domain
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.mobile_car = t2.mobile_car
I'm also assuming that your actual table structure will be more complex than this, since right now you can actually just query Table 2 by itself to get what you need.
I have a database with two tables. The first one contains the user_name, user_password, user_email. The second one contains the user_name, user_age, user_description.
When a person finds the user he needs by the user_name, the script looks through the database using the user_name, to give out the information about certain user.
But if the person changes his user_name via preferences, the value changes only in the first table.
Question:
1) Is there a way to make the user_name in the second table change automatically? (To connect them some how)
I am using MySQL (phpMyAdmin).
This is just a simple example. In "real world" I am trying to manage more serious applications that have more tables. Is there an easier way than to create a separate php query for each table?
You could always create an AFTER UPDATE MySQL trigger targeting single rows for this. See the manual. It's probably not easier than using separate PHP queries for the tables, though. You don't need to spell them all out, just map up what needs to be synchronized when, and abstract your code.
However I'd recommend that you use a unique ID field for the user and only store the username in one of the tables -- and refer to the user with the ID under the hood of your code, and in both tables. Not a good idea to use something changeable as a unique identifier in your database design.
I made a mysql database for my project. I need to know if what I did is correct or not.
I made a master table to store all my movie details and I built some other tables to store the views and ratings. Am I doing wrong by using different tables to store views and ratings, although I can use the master table to store views and rating in each post.
If I add the rating and views fields to the master table itself, will it affect my database? What's the optimal solution?
I am using Myisam as lots of select queries are needed instead of insert.
If you are just using the fields for incremental value, you can use the master table.
But if you wish to save extra parameters to a vote or view, you should use a secundary table.
e.g. You want to save which user voted which value on which date. Than you should add a table 'votes' with the fields: voteId, userId, rating, date.
Afterwards you can use the AVG function to calculate the avarage vote.