Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to add a computeed column.
alter table datatest add column amount2 double as (amount*rate)
but I got error while executing this
MySQL doesn't support computed columns prior to MySQL 5.7. The more recent versions do now support computed columns.
You can use a view instead:
create view v_datatest as
select t.*, (amount * rate) as amount2
from datatest;
Notes:
In databases that do support computed columns, the type is not part of the column definition. It is derived from the expression (you can use cast()/convert() to convert to a particular type).
It is a bad idea to store monetary amounts using floating point representations. You should be using decimal/numeric instead.
If you don't want to use a view, you can add a column to the table (along with the type) and use a trigger to maintain the value.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables that are accessed as individual feeds and also a group feed.
For individual feeds, I can implement cursor-based pagination based on each row's unique id.
How would I implement cursor-based pagination for the group feed, which basically combines all 3 tables into 1 feed?
Each table has unique ids and a timestamp for when it was created (although this is not unique).
I've considered using the timestamp as some sort of pointer, for example, results after a particular timestamp, but this could lead to missing results, as if you requested 10 rows, after a timestamp, and these rows all had the same timestamp, as did another 20 rows, when you perform a subsequent request, you will miss those following 20 rows.
How can this problem be tackled?
Window functions.
MySQL 8.0 introduced support for standard SQL window functions. See https://dev.mysql.com/doc/refman/8.0/en/window-functions.html
SELECT *
FROM (
SELECT ..., ROW_NUMBER() OVER () AS rownum
FROM <multiple tables joined>
) AS t
WHERE rownum BETWEEN ? and ?
No need for LIMIT. You just use parameters to select the range of rows corresponding to the current "page" you want to view.
If you answer "but I haven't upgraded to MySQL 8.0 yet," then I would say now you have a good reason to upgrade.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Using MySQL, I want to retrieve all rows from a table A where a field contains "EQ" for example, copy them into table B (which has exactly the same field), and then delete them from table A.
I heard about triggers. Is that the right approach?
You need 2 SQL statements. First for inserting the data rows from A to B, and then for deleting those rows from A.
INSERT INTO B
SELECT *
FROM A
WHERE Field LIKE '%EQ%';
DELETE
FROM A
WHERE Field LIKE '%EQ%';
Triggers are a way to automate some activities when some other activity in a separate table happens. If you need to make sure that the above 2 operations should be triggered as soon someone INSERTS/DELETES/UPDATES on some other table, then you can create a trigger for that purpose. But with the simple requirement you gave above without any such dependency, I do not see a need of a trigger here.
If you do have such dependency and need, you have to provide proper requirements with details here.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Essentially I want to create a view in mySQL (phpmyAdmin) that queries a table called Equipment for a date range >=2018-12-1.
This is currently what I have, it is what is wrong with my syntax?
CREATE VIEW "Equipment_Date" AS SELECT * FROM "Equipment"
WHERE "Ship_Del_Date" >= 2018-12-1;
Below query should work provided Ship_Del_Date column has datatype as DATETIME.
CREATE VIEW Equipment_Date AS SELECT * FROM Equipment
WHERE Ship_Del_Date >= '2018-12-1';
Use backticks instead of single quotes to enclose the table,column names only when the names are from mysql reserved keywords.
Use these to specify databases, tables and columns: `
And not these: "
Or simply just don't use any of these, if not necessary.
Then your SQL query will probably look like this:
CREATE VIEW Equipment_Date AS SELECT * FROM Equipment WHERE Ship_Del_Date >= 2018-12-1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm using MYSQL to store data about user orders. Using PayPal, some orders have only a transaction ID, and others have only a profile ID. Is it poor database design to have separate fields for txn_id and profile_id, where one or the other is NULL depending on the order?
No, that is not poor design at all.
You have a situation where one or the other is NULL depending on the context. The problem arises when you try to enforce that exactly one is NULL or at least one is not NULL. To do that effectively, you will need to use a trigger to check the values.
If there is always only one id or the other, you might consider having two fields, one is something like id_type and the other is id_value (if they are of the same data type).
This might make querying simpler down the line.
You have two options basically:
Two fields, one for Transaction and one for Profile
One field for both Transaction and Profile and have another field to identify whether is a Transction ID or a Profile ID
I prefer No. 1 (which basically the same as yours) because you maintained the proper naming of the field and not a generic one. And besides you could always use COALESCE() or that kind of function to retrieve the not NULL one if either is NULL.
On that regard it means also that you should have a artificial primary key that would not rely on either Transction ID or Profile ID.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Our code is using create/drop table, while generating VR4 queue orders in our database.
When number of websites is less than 250, code is using IN operator and generating reports. ce.website_id in (" . (join ",", #{$website_id}) . ")
When we have more than 250 websites, our code is creating tables (name like Temp_tablename) and using table joining instead of IN operator. Can I replace this code to use IN operator as well? Will there be any performance issue, if IN operator is used with more input values?
As mentioned by Stan, using a temporary table rather than a large IN is the preferred way to go.
When MySQL gets a large data block from the user it stores it in a temporary table and uses a JOIN to look through it. This is easier for MySQL to do than to actually look for each of your values in the IN SQL part.
You can skip this temporary table, by first storing in a table your web site list:
REPLACE INTO tblWebSitesToHandle
(Session_ID, WebSite)
VALUES
('**unique_number**', '**website_id**'),
('**unique_number**', '**website_id**'), ...
Where unique_number will be some number you chose, and then toss away once the query ends - but it will help you manage the list of websites to handle for your query
Then in your SQL that you are currently using instead of IN (...) you will do a JOIN to this table and select from it the relevant Session_ID record.
After that is done, just remove from tblWebSitesToHandle the Session_ID data, it is no longer needed (I believe).