I'm a newbie in database especially MYSQL. I have difficulties in how to select and put the result into a column in same table.
I have a table ( production) with column (name,price,item,total)
Name
price
item
total
book1
100
2
book2
200
3
book3
300
1
book4
400
2
book5
500
1
I have query to select :
SELECT Name,
price*item AS total
FROM production ;
but how to put the result into total column?
before, I tried directly
UPDATE production
SET total = price*item;
but there's an error :
SQL Error [1292] [22001]: Data truncation: Truncated incorrect DOUBLE value: ' '
Please help me ... I'm already looking this solution for days but still no luck, thank you and really appreciate.
Related
select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"
your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server
I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.
I am writing a query sql for a data migration job to fetch data and send from mysql-server a to mysql-server b.
Server a has different databases, representing different game channel, and each database has a table tablex, they have same table name and same schema:
uid level
123 3
211 5
While in server b there is only one table tablex to receive tablex of all databases and it has one more column - channel
channel uid level
1 123 3
1 211 5
2 355 2
I can parse channel number from db name via python but I need to put this constant in the sql and since there are many tables, I cannot fix the columns. So just want to make sure is there any way to do this like:
select 1,* from xxx.yyy
You could try adding alias and table name
select 1 as my_col, yyy.* from xxx.yyy
or using string
select cast('1' as unsigned) , yyy.* from xxx.yyy
Yes, you can wrap the query into another SELECT and add the columns, e.g.:
SELECT A.*, '1'
FROM (
SELECT *
FROM your_table
) A;
I have the below scenario. I have a table(name mytab1) in mysql database(using workbench community 6.3) which consists of columns like id, message and incidentdate(which is a date-time type). Mytab1 can contain more than 10k data. Now my requirement is to fetch the data by below condition :
Edit :
if{incidentdate < (current_systime - 2 hours)}
then fetch all the records from the table.
Else fetch the record whose incidentdate < (current_systime - 5 minutes).
I have used the below syntax for the same :
select if((select timestampdiff(minute,incidentdate,now()) from mytab1)>120,
(select message from mytab1),
(select message from mytab1 where incidentdate < date_add(now(), interval -5 minutes)))
as message;
Now, for the very first instance my table mytab1 contains below data :
id, message, incidentdate
-------------------------
A01, Incident_01, 2016-02-08 20:00:30
A02, Incident_02, 2016-02-08 20:10:20
conditional SQL query should able to fetch the above two data(as it is beyond two hours of current system time i.e. 2016-02-09 01:00:10). Now in the second instance a new data(one row for example) has entered in mytab1 table and mytab1 looks like below :
id, message, incidentdate
-------------------------
A01, Incident_01, 2016-02-08 20:00:30
A02, Incident_02, 2016-02-08 20:10:20
A03, Incident_03, 2016-02-09 00:55:30
And with this new data conditional SQL query should able to fetch data for complete third row only having message as "Incident_03".
With the above mentioned syntax of SQL query I am not able to fetch the data if there are multiple row in mytab1 and also not working if I want to fetch all the columns of mytab1 table. Would you kindly help with the conditional SQL syntax for this requirement please.
I have a query:
select lr2.event_id as ce
from data_requests as lr2
group by lr2.event_id,
that returns 88 rows. Then I tried the following:
select count(lr2.event_id) as cc, lr2.event_id as ce
from data_requests as lr2
group by lr2.event_id
but it only returned 25 rows, so I am really puzzled, where did other 63 rows go.
I tried it in sqlfiddle, it seems to work correctly, but on my server it just doesn't, so it must be a setting or something... Feels like the server calculates the count after it select a subset of all group results. weird.
if you want to count the number of rows for each lr2.event_idyou must use count(*) , not count(lr2.event_id) . Remember, you are counting rows.
Function of GROUP BY
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical/similar/equal data into groups.
Demonstration
If I have table like below, then 1st query will give same output as table definition:
ce
--
1
2
3
4
5
And 2nd query will give output as,
cc |ce
--- ---
1 1
1 2
1 3
1 4
1 5
Since, all are distinct in Table, I got 5 rows! but If some ce values are repetitive as,
ce
--
1
2
1
2
2
then, 2nd query will give output as:
cc |ce
--- ---
2 1
3 2
And here If I get shocked where did other 3 rows go? Then I need to study!
Of course, it's a spoon feeding! OP needs to study about GROUP BY in SQL.
My bad, it seems to be a phpmyadmin problem, I run the query in the phpmyadmin, and it auto added a limit in the end of every query
I have a MySQL database where one column contains status codes. The column is of type int and the values will only ever be 100,200,300,400. It looks like below; other columns removed for clarity.
id | status
----------------
1 300
2 100
3 100
4 200
5 300
6 300
7 100
8 400
9 200
10 300
11 100
12 400
13 400
14 400
15 300
16 300
The id field is auto-generated and will always be sequential. I want to have a third column displaying a comma-separated string of the frequency distribution of the status codes of the previous 10 rows. It should look like this.
id | status | freq
-----------------------------------
1 300
2 100
3 100
4 200
5 200
6 300
7 100
8 400
9 300
10 300
11 100 300,100,200,400 -- from rows 1-10
12 400 100,300,200,400 -- from rows 2-11
13 400 100,300,200,400 -- from rows 3-12
14 400 300,400,100,200 -- from rows 4-13
15 300 400,300,100,200 -- from rows 5-14
16 300 300,400,100 -- from rows 6-15
I want the most frequent code listed first. And where two status codes have the same frequency it doesn't matter to me which is listed first but I did list the smaller code before the larger in the example. Lastly, where a code doesn't appear at all in the previous ten rows, it shouldn't be listed in the freq column either.
And to be very clear the row number that the frequency string appears on does NOT take into account the status code of that row; it's only the previous rows.
So what have I done? I'm pretty green with SQL. I'm a programmer and I find this SQL language a tad odd to get used to. I managed the following self-join select statement.
select *, avg(b.status) freq
from sample a
join sample b
on (b.id < a.id) and (b.id > a.id - 11)
where a.id > 10
group by a.id;
Using the aggregate function avg, I can at least demonstrate the concept. The derived table b provides the correct rows to the avg function but I just can't figure out the multi-step process of counting and grouping rows from b to get a frequency distribution and then collapse the frequency rows into a single string value.
Also I've tried using standard stored functions and procedures in place of the built-in aggregate functions, but it seems the b derived table is out of scope or something. I can't seem to access it. And from what I understand writing a custom aggregate function is not possible for me as it seems to require developing in C, something I'm not trained for.
Here's sql to load up the sample.
create table sample (
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
status int
);
insert into sample(status) values(300),(100),(100),(200),(200),(300)
,(100),(400),(300),(300),(100),(400),(400),(400),(300),(300),(300)
,(100),(400),(100),(100),(200),(500),(300),(100),(400),(200),(100)
,(500),(300);
The sample has 30 rows of data to work with. I know it's a long question, but I just wanted to be as detailed as I could be. I've worked on this for a few days now and would really like to get it done.
Thanks for your help.
The only way I know of to do what you're asking is to use a BEFORE INSERT trigger. It has to be BEFORE INSERT because you want to update a value in the row being inserted, which can only be done in a BEFORE trigger. Unfortunately, that also means it won't have been assigned an ID yet, so hopefully it's safe to assume that at the time a new record is inserted, the last 10 records in the table are the ones you're interested in. Your trigger will need to get the values of the last 10 ID's and use the GROUP_CONCAT function to join them into a single string, ordered by the COUNT. I've been using SQL Server mostly and I don't have access to a MySQL server at the moment to test this, but hopefully my syntax will be close enough to at least get you moving in the right direction:
create trigger sample_trigger BEFORE INSERT ON sample
FOR EACH ROW
BEGIN
DECLARE _freq varchar(50);
SELECT GROUP_CONCAT(tbl.status ORDER BY tbl.Occurrences) INTO _freq
FROM (SELECT status, COUNT(*) AS Occurrences, 1 AS grp FROM sample ORDER BY id DESC LIMIT 10) AS tbl
GROUP BY tbl.grp
SET new.freq = _freq;
END
SELECT id, GROUP_CONCAT(status ORDER BY freq desc) FROM
(SELECT a.id as id, b.status, COUNT(*) as freq
FROM
sample a
JOIN
sample b ON (b.id < a.id) AND (b.id > a.id - 11)
WHERE
a.id > 10
GROUP BY a.id, b.status) AS sub
GROUP BY id;
SQL Fiddle