I want to copy profiling data to a table I create in MySql.
I want the table to contain the exact data that I get from the command SHOW PROFILES;
For example, if I have this:
mysql> show profiles;
+----------+------------+--------------------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------------------+
| 16 | 0.00059700 | select * from imprumuturi |
| 17 | 0.00042050 | select * from imprumuturi |
| 18 | 0.00042000 | select * from imprumuturi |
| 19 | 0.00042950 | select * from imprumuturi |
| 20 | 0.00048050 | select * from imprumuturi |
+----------+------------+--------------------------------+
5 rows in set (0.00 sec)
I will create a table that has 3 columns (queryid,duration and query), and I need a command that will copy those 5 rows from "SHOW PROFILES;" to my table.
insert into table (show profiles); does not work
I need it to be done in MySql alone, no other tools/patches.
INSERT INTO t (SELECT some columns FROM INFORMATION_SCHEMA.PROFILING)
insert into t (select * from information_schema.profiling)
Related
You can easily generate a one row result-set with a MariaDB/MySQL query like so:
MariaDB [(none)]> SELECT 'Some value' AS some_col_name;
+---------------+
| some_col_name |
+---------------+
| Some value |
+---------------+
1 row in set (0.000 sec)
However, I would like to do a similar thing, but generate multiple rows of data.
I came up with this:
SELECT 'row1-value1' AS col_name_1 , 'row1-value2' AS col_name_2, 'row1-value3' AS col_name_3 UNION ALL SELECT 'row2-value1' AS col_name_1, 'row2-value2' AS col_name_2, 'row2-value3' AS col_name_3;
+-------------+-------------+-------------+
| col_name_1 | col_name_2 | col_name_3 |
+-------------+-------------+-------------+
| row1-value1 | row1-value2 | row1-value3 |
| row2-value1 | row2-value2 | row2-value3 |
+-------------+-------------+-------------+
2 rows in set (0.000 sec)
Which works, but the query isn't very nice. Is there a better way to do it either in MySQL or MariaDB?
The VALUES statement is new in MySQL 8.0:
VALUES ROW('row1-value1', 'row1-value2', 'row1-value3'),
ROW('row2-value1', 'row2-value2', 'row2-value3');
Output:
+-------------+-------------+-------------+
| column_0 | column_1 | column_2 |
+-------------+-------------+-------------+
| row1-value1 | row1-value2 | row1-value3 |
| row2-value1 | row2-value2 | row2-value3 |
+-------------+-------------+-------------+
See https://dev.mysql.com/doc/refman/8.0/en/values.html
There doesn't seem to be any syntax in the VALUES statement to set the column names. The columns are named implicitly column_N starting with 0.
You can name the columns with a workaround: use VALUES after UNION with another query, because the first query in a UNION determines the column names. Below is an example that uses another new feature of MySQL 8.0, applying a WHERE condition to a SELECT statement without any table reference. The SELECT therefore returns zero rows, and its only purpose is to name the columns.
mysql> SELECT null as a, null as b, null as c WHERE FALSE
UNION VALUES ROW('row1-value1', 'row1-value2', 'row1-value3'),
ROW('row2-value1', 'row2-value2', 'row2-value3');
+-------------+-------------+-------------+
| a | b | c |
+-------------+-------------+-------------+
| row1-value1 | row1-value2 | row1-value3 |
| row2-value1 | row2-value2 | row2-value3 |
+-------------+-------------+-------------+
You can use a CTE where you name the columns that you want and generate the column values with the VALUES statement and the ROW() row constructor clause:
WITH cte(col_name_1, col_name_2, col_name_3) AS (VALUES
ROW('row1-value1', 'row1-value2', 'row1-value3'),
ROW('row2-value1', 'row2-value2', 'row2-value3')
)
SELECT * FROM cte;
Result:
col_name_1
col_name_2
col_name_3
row1-value1
row1-value2
row1-value3
row2-value1
row2-value2
row2-value3
See the demo.
+----+---------+--------------------+----------+------+---------------------+---------+-------------+------------+----------+
| id | pname | pmail | pgender | page | pdetails | pnumber | ddepartment | date | time |
+----+---------+--------------------+----------+------+---------------------+---------+-------------+------------+----------+
| 1 | karuna | karuna#gmail.com | female | 21 | hfsdh gfhdf shdh | 2332 | Surgeon | 2018-05-15 | 16:00:00 |
+----+---------+--------------------+----------+------+---------------------+---------+-------------+------------+----------+
This is my database table appointment.
Whenever I execute first two queries, they run perfectly
select * from appointment;(execute )
select * from appointment where pname = 'karuna';(execute)
but when I try to execute these two queries, the result I get is empty set
select *from appointment where pmail='karuna#gmail.com';(empty set (0.00 sec))
select *from appointment where ddepartment='Surgeon';(empty set (0.00 sec))
Hey I need to make a MySql query and get from it some number of user activities, lets say 10, then after scrolling on page I need to take another portion of activities stored in DB and start from 10 to 20 and so on... As I made this already by loading the whole DB Content for user and then dynamically show it with AJAX and jQuery I need to change the method I am doing this. So my query looks like this:
SELECT some rows FROM table WHERE User_ID = #memberID ORDER By date LIMIT limit
As this query works to take only limited records from DB I have no idea how to make a parameter that would determine which records should we take now. The problem starts when user refreshes the page - we want to start from 0 and again go 10 by 10 down.
EDIT: I am giving the query 2 params (LIMIT and OFFSET) and then in jQuery function gonna try to increase both of them.
you can do it like this
example
mysql> SELECT * FROM MAXWELL;
+------+-------+
| ID | NAME |
+------+-------+
| 3 | TWO |
| 4 | FOUR |
| 5 | FIVE |
| 6 | SIX |
| 7 | SEVEN |
+------+-------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM MAXWELL limit 0,2;
+------+------+
| ID | NAME |
+------+------+
| 3 | TWO |
| 4 | FOUR |
+------+------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM MAXWELL limit 2,4;
+------+-------+
| ID | NAME |
+------+-------+
| 5 | FIVE |
| 6 | SIX |
| 7 | SEVEN |
| 10 | ten |
+------+-------+
4 rows in set (0.00 sec)
I am having a table below, and I need to write code that extract the rows with budget greater than the average budget.
+------+-----------------+--------+
| Code | Name | Budget |
+------+-----------------+--------+
| 14 | IT | 65000 |
| 37 | Accounting | 15000 |
| 59 | Human Resources | 240000 |
| 77 | Research | 55000 |
+------+-----------------+--------+
I know this works:
SELECT * FROM Departments WHERE Budget > (SELECT AVG(Budget) FROM Departments);
but this looks ugly. This post seems to suggest having clause can simplify the query into:
SELECT * FROM Departments HAVING Budget > AVG(Budget);
but it returns empty set. Any ideas?
Thanks
This is because AVG() is aggregation function which should be used GROUP BY or with other Aggregation functions.
If not, SELECT would returns single row. for example:
mysql> SELECT * FROM test;
+------+--------+
| code | budget |
+------+--------+
| 14 | 65000 |
| 37 | 15000 |
| 59 | 240000 |
| 77 | 55000 |
+------+--------+
4 rows in set (0.00 sec)
mysql> SELECT code, budget, AVG(budget) FROM test;
+------+--------+-------------+
| code | budget | AVG(budget) |
+------+--------+-------------+
| 14 | 65000 | 93750.0000 | we got one row.
+------+--------+-------------+
1 row in set (0.00 sec)
In this case, HAVING budget > AVG(budget) means 65000 > 93750 which is false, so that returns empty list.
Your first attampt does not look like 'ugly' ;)
In mySQL, having an aggregation column with SELECT * will return the first row only.
This SQL Fiddle shows that:
SELECT *, AVG(BUDGET) FROM Departments;
will return only the first row and the average of budget of all rows.
Then, as in your first row, the budget is smaller than the average of budgets, it will return no rows.
I believe your UGLY (I don't think it is ugly) query is a good solution for this.
I'm trying to get a query to get the batchuuid from the batchTBL
that aren't in the JobBatchStatusTBL..
I've tried a couple of different queries trying to use something like:
select *
from BatchTBL as ba
left join JobBatchStatusTBL as j
on ba.BatchUUID=j.BatchUUID
join JobTBL as j2
on j.JobUUID=j2.JobUUID
where j.batchuuid IS NULL
and j.JobUUID = 'ecd0fab8-8bf1-83cc-b1d7-495034a55618';
but i'm screwing something up...
any thoughts?
thanks
mysql> select BatchName,BatchUUID from BatchTBL;
+-----------+--------------------------------------+
| BatchName | BatchUUID |
+-----------+--------------------------------------+
| aa | d288ff51-d045-d218-52fd-93e3523db85e |
| aa1 | a288ff51-d045-d218-52fd-93e3523db85e |
| aa3 | d188ff51-d045-d218-52fd-93e3523db85e |
| aaa3 | da88ff51-d045-d218-52fd-93e3523db85e |
| baa3 | db88ff51-d045-d218-52fd-93e3523db85e |
| z1 | 7eedfea4-c498-ed6e-f0dd-1397fe7dbd67 |
| d1 | 34781dba-d99c-82e3-f499-b55ded863f81 |
| nb | 1dd56d9c-daed-7f9f-c13b-246d2ec96513 |
| ds | cca9a771-b5ef-5926-4c26-21151215a800 |
| a1 | 1bb51584-e68a-21d1-a2df-e07707591b43 |
+-----------+--------------------------------------+
10 rows in set (0.00 sec)
mysql> select JobName,JobUUID from JobTBL;
+---------+--------------------------------------+
| JobName | JobUUID |
+---------+--------------------------------------+
| aa | 8afa9cf4-bf63-a4cd-3cd9-cbc6d17f84be |
| aa1 | ecd0fab8-8bf1-83cc-b1d7-495034a55618 |
+---------+--------------------------------------+
2 rows in set (0.00 sec)
mysql> select JobUUID,BatchUUID from JobBatchStatusTBL;
+--------------------------------------+--------------------------------------+
| JobUUID | BatchUUID |
+--------------------------------------+--------------------------------------+
| ecd0fab8-8bf1-83cc-b1d7-495034a55618 | d288ff51-d045-d218-52fd-93e3523db85e |
+--------------------------------------+--------------------------------------+
1 row in set (0.00 sec)
thanks
Your query puts an inner join on JobBatchStatusTbl to JobTbl, so rows that are in one but not the other will never be returned at all. You only need two of the tables for this:
SELECT *
FROM BatchTbl
WHERE JobUUID
NOT
IN (SELECT JobUUID
FROM JobBatchStatusTBL)
I should note that it's impossible to use the JobUUID in the WHERE clause as you're attempting in your initial query, because this is returning only batches for which there is no corresponding job, according to your post - which makes me wonder if your post was misworded, since you had a specific job uuid in your query?
On another note, you should never name tables, columns or anything else in a way that describes their schema type.
Try
select * from BatchTBL where JobUUID not in (select JobUUID from JobBatchStatusTBL)
If I understand what you are trying to do correctly. To "get a query to get the batchuuid from the batchTBL that aren't in the JobBatchStatusTBL"
use the not in SQL syntax. It is easier to write and read, and the optimizer will take care of the rest.
Select BatchName,BatchUUID from
BatchTBL where BatchUUID not in
(Select BatchUUID from JobBatchStatusTBL)