I am trying to get the average of the columns in my table and then insert the averages into a second table, I have over 30 columns so I would rather not have to do them all individually if possible
command.CommandText = " INSERT INTO FaceAverages(`rightEyeRightUpper`),(`rightEyeLeftUpper`),(`rightEyeRightLower`),(`rightEyeLeftLower`),(`leftEyeRightUpper`)... FROM(SELECT AVG (rightEyeRightUpper),(rightEyeLeftUpper),(rightEyeRightLower),(`rightEyeLeftLower`)... FROM 'FaceDistancesHappy')";
Assuming you table FaceAverages contains the coulmns
`rightEyeRightUpper`,`rightEyeLeftUpper`,`rightEyeRightLower`,
`rightEyeLeftLower`,`leftEyeRightUpper`...
then you could use an insert select as
command.CommandText = " INSERT INTO FaceAverages(`rightEyeRightUpper`,`rightEyeLeftUpper`,
`rightEyeRightLower`, `rightEyeLeftLower`,`leftEyeRightUpper`... )
SELECT AVG(rightEyeRightUpper), AVG(rightEyeLeftUpper),
AVG(rightEyeRightLower), AVG(`rightEyeLeftLower`), ...
FROM 'FaceDistancesHappy'";
declaring all the column you want insert crresponding to all the column you want select . and using the AVG() function for each column in select
Related
Each Lead put data into pre_order_quotation table with a new version each time.
I am trying to get row count of all quotation by a lead & add 1 to it to get the version of new entry of quotation .
Was trying to achieve it in single query
insert into pre_order_quotation (lead_id,version,created_at,updated_at,file_name) values (1405,(select count(*) from pre_order_quotation where lead_id = 1405)+1,'2020-08-22 12:13:51','2020-08-22 12:13:51','dummy-5f410bffbcc80.pdf')
But i am getting the following error :
You can't specify target table 'pre_order_quotation' for update in FROM clause
How can i achieve it in single query?
Bury the increment a bit deeper..
insert into pre_order_quotation (lead_id,version,created_at,updated_at,file_name)
values (1405,
(select cnt from (select count(*) + 1 cnt from pre_order_quotation where lead_id = 1405) s ),
'2020-08-22 12:13:51','2020-08-22 12:13:51','dummy-5f410bffbcc80.pdf');
I have two table: employee and privremeno and the both of them contains column jmbg. I want insert in employee (two columns) data from privremeno (two columns) so that data would be inserted in row where jmbg in employee is equal (the same) to jmbg in privremeno.
Something like:
INSERT INTO vg_pka.employee (stazDani, godZivota) select ukstaz, gz from
vg_pka.privremeno where vg_pka.privremeno.jmbgl = vg_pka.employee.jmbg;
How to do that?
you have to use update to inser values to existing table . I cant get your correct requirement . But the below is the syntax you have to use.
update table_name set = 'value' where (your condition)
You can use a join:
INSERT INTO
vg_pka.employee (stazDani, godZivota)
SELECT
ukstaz, gz
FROM
vg_pka.privremeno p
INNER JOIN
vg_pka.employee e
ON
p.jmbgl = e.jmbg;
I am new to SQL and i want to copy distinct dates from a column named starttime in a table user in a database MyDb, to a column playing_date in a table collection in another database named viral.
I have used the following query but its not working:
mysqli_select_db($con,'MyDb');
$query1 = "INSERT INTO viral.collection.playing_date SELECT DISTICT date(starttime) FROM user";
if(mysqli_query($con,$query1))
echo "Successfully Inserted";
else
echo "error";
How should i correct it?
You are missing the N in DISTINCT
INSERT INTO viral.collection.playing_date SELECT DISTINCT date(starttime) FROM user
SELECT DISTICT date(starttime) into viral.collection.playing_date FROM user;
this will work!
I have a query for insert into table using sub query prob is that in sub query there
is 2 columns and with where condition and group by clause.
sub query is running well can any one help me plz
Query : Account_name is type text
insert into trial_bal (Account_name,Debit) values (
select convert(text,convert(varchar(max),Accounts)),SUM(ISNULL( Debit,0))-SUM( ISNULL(Credit,0))
from general
where Acount_Type='Assets'
group by convert(varchar(max),Accounts)
);
Instead of using INSERT INTO ..VALUES you should use INSERT INTO .. SELECT..FROM to return the values from the select statement:
insert into trial_bal (Account_name,Debit)
select convert(text,convert(varchar(max),Accounts)),
SUM(ISNULL( Debit,0))-SUM( ISNULL(Credit,0))
from general
where Acount_Type='Assets'
group by convert(varchar(max),Accounts)
Is there anyway to get the seed fieldname?
What I mean by seed is the field that's been created with something like this;
INT NOT NULL AUTO_INCREMENT PRIMARY KEY
I plan on using this ( getting the seed field name pro grammatically ) in coming up with the fastest SQL query to get the number of records in a table.
The function I plan on writing is something like this. Please fill in the blanks and provide the getSeed function inner mechanics.
function get_record_count ($dbh,$table,$where){
//get the seedfield name in the {table} programmatically
$seed = getSeed($dbh,$table);
$sql = "select count({$seed}) as `count` from {$table} " . $where;
//do the mysql query & get num rows to return it...
}
If you're not trying to count the number of non-NULL values in the column (COUNT(expr) doesn't count NULLs), then just use SELECT COUNT(*) and let MySQL use the same index that is used in the WHERE clause to answer COUNT(*).
$sql = "SELECT COUNT(*) AS `count` FROM {$table} " . $where;