PHP mysql table move to table - php

I have 2 tables which are daily_matches and archived_matches. I want to move daily_matches' rows to archived_matches. But archived_matches has one more column which is match_date. I also want to insert yesterday date to match_date when i am moving the daily_matches table.
$sql = "INSERT INTO archived_matches
(league, hour, code, team_home, team_away, result, rate_home, rate_draw_, rate_away)
SELECT league, hour, code, team_home, team_away, result, rate_home, rate_draw_, rate_away FROM daily_matches";
Above codes does not insert the match_date. Also that code does not work. It does not move the table. How can i move table and insert match_date ?
Thanks in advice.

If you want insert also match date you should add the related cols in insert and select eg:
$sql =
"INSERT INTO archived_matches
(league, hour, code, team_home, team_away, result, rate_home,
rate_draw_, rate_away, match_date)
SELECT league, hour, code, team_home, team_away, result, rate_home,
rate_draw_, rate_away, subdate(current_date, 1)
FROM daily_matches";

Related

Using Insert with 2 selects inside in php

Here's what i'm trying to do
$sql = "INSERT INTO LocationGeofencePoint( PointNo, Latitude, Longitude)
VALUES ( (SELECT LocationGeofenceID FROM LocationGeofence WHERE GeofenceName='".$name."'),
(SELECT MAX(PointNo)+1 FROM LocationGeofencePoint),
'".$lat."',
'".$long."')";
But this happens.
Every time I add "" the 2nd select stops working.
These are 2 separate tables LocationGeofencePoint and LocationGeofence and I'm trying to get values from LocationGeofence into LocationGeofencePoint.
The result should be something like
LocationGeofencePoint(2 , 14, 54.333, 55.435) (these would be the values)
LocationGeofence(2, Test , group1) (trying to get this '2' into location geofencepoint)
I also tried it just in case it was my editor bugging out but it isn't and i've run out of ideas , any idea on what I can do ? I tried separating the queries but I also need 2 selects to run the 2nd one and that isn't working.
$sql = "INSERT INTO LocationGeofencePoint( PointNo, Latitude, Longitude)
VALUES ( (SELECT LocationGeofenceID FROM LocationGeofence WHERE GeofenceName='".$name."'),
(SELECT MAX(PointNo)+1 FROM LocationGeofencePoint),
'".$lat."',
'".$long."')";
Put same number of column in insert fields and as well as its column values.
First, GeoReferencePoint should be declared as an identity column. You should not be incrementing the value on insert. The database does that for you.
Then, you should be using insert . . . select:
$sql = "INSERT INTO LocationGeofencePoint (PointNo, Latitude, Longitude)
SELECT LocationGeofenceID, $lat, $long
FROM LocationGeofence
WHERE GeofenceName = '".$name."';
And third, this is not right either. The three values being passed in should be using parameters.
Do you mean 'stop working' as the color is not the same color with the first SELECT?
If so, don't worry it just the editor's problem. You will achieve the desired result once you run the code. I experience the same before.

How do I combine these two queries into a single INSERT INTO?

Can someone tell me how can I combine these two queries into a single INSERT INTO query?
$query1 = "INSERT INTO `monthly_income`(`Year`, `Month`) VALUES (2017, 'September')";
$query2 = "INSERT INTO `monthly_income`(`totalincome`) SELECT SUM(Income) FROM `eventincome` WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-30'";
Here I want to insert all those values into a single row in the table "monthly_income". In Query2, i generate the total income between two dates from a seperate table called "eventincome". The "monthly_income" table has columns [Year, Month, totalincome]. And the "eventincome" table has columns [eventDate, eventTitle, Earnings, Expenses, Income]. So how can I join these two querys to use it in a
mysqli_query($conn, parameter)
:)
PS: I want to set those values from those queries in the same row, without creating two seperate rows becuse of those two INSERT INTO's. That's why I wanna join/combine/merge(whatever) those two queries :)
Give this a try
$query2 = "INSERT INTO `monthly_income`(`totalincome`,`Year`, `Month`) SELECT SUM(Income),2017 as Year, 'September' as Month FROM `eventincome` WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-30'";
Add the values from the first query as constants in the second query...
"INSERT INTO monthly_income(Year, Month,totalincome) SELECT 2017, 'September',SUM(Income) FROM eventincome WHERE eventDate BETWEEN '2017-09-01' AND '2017-09-3'";

Insert data with MAX(id) and values of status at the same time

I was trying with this code but it didn't work. it's always get the MAX(eq_no) as 0
$sql1 =mysqli_query($con, "SELECT MAX(eq_no) AS val FROM tech_add_equip");
$sql2 = "INSERT INTO time (eq_no,status_no) VALUES ('$val', 4 );";
if (!mysqli_query($con,$sql2)) {
die('Error: ' . mysqli_error($con)); };
Finally, after I try with this code, it inserts in the right number of MAX(eq_no) but i still cant insert the values of status_no
INSERT INTO time (eq_no) SELECT MAX(eq_no) AS vale FROM tech_add_equip
Could you suggest me what did i missing in the code?
Thank you for your helping
One row returned from SELECT a,b,c statement in sub query is equivalent to set of values that is otherwise hardcoded as ('a-value','b-value','c-value')*. You can hardcode a value within select as well:
INSERT INTO time (eq_no, status_no)
SELECT MAX(eq_no), 4
FROM tech_add_equip
No need for aliases within select - order of columns matters.
*) One row result can be used for IN() clause. Another row would become set of values after comma - can't be uset for IN(), but it works ok for INSERT
('row1-a-value', 'row1-b-value'), ('row2-a-value', 'row2-b-value')
$max = SELECT MAX( customer_id ) FROM customers;
INSERT INTO customers( customer_id, statusno )
VALUES ($max , 4)

insert value from one table into another whilst doing a double insert

Not sure that this is even possible. I am inserting values into two tables at the same time using multi_query. That works fine. One of the tables has an auto increment column and I need to take the last auto incremented number and insert it into the second table so like this: insert into table 1 then take the last inserted number from column X and insert it along with other data into table 2. I have played around with using SELECT LAST and INSERT INTO but so far its just doing my head in. The insert statements looks like this:
$sql= "INSERT INTO tbleClients (riskClientId, riskFacility, riskAudDate) VALUES ('$riskclient2', '$facility2', '$riskdate2');";
$sql .="SELECT LAST(riskId) FROM tbleClients;";$sql .="INSERT INTO tblRiskRegister (riskId) SELECT riskId FROM tbleClients ;";
$sql .= "INSERT INTO tblRiskRegister (riskAudDate, riskClientId, riskSessionId, RiskNewId) VALUES ('$riskdate2', '$riskclient2', '$sessionid', '$risknewid')";
Individually they all produce results but I need it to happen simultaneously. I did toy with the idea of doing them all separately but figure thats not very efficient. Any pointers would be appreciated.
$sql= "INSERT INTO tbleClients (riskClientId, riskFacility, riskAudDate) VALUES ('$riskclient2', '$facility2', '$riskdate2');";
After executing the above query, using mysqli_insert_id(), which gives you the last insert id.
So below query is useless.
$sql .="SELECT LAST(riskId) FROM tbleClients;";
$sql .="INSERT INTO tblRiskRegister (riskId) SELECT riskId FROM tbleClients ;";
You can insert last_insert_id in above query.
Unable to find the relation between above & below query.
$sql .= "INSERT INTO tblRiskRegister (riskAudDate, riskClientId, riskSessionId, RiskNewId) VALUES ('$riskdate2', '$riskclient2', '$sessionid', '$risknewid')";

Access last row added to table - PHP

I have a simple sql query adding a new row to a database and need it to return the a field back to Javascript. The field does Auto_increment but stupildy I called it 'itemId' so mysql_insert_id doesnt work and I don't think I have time to go and amend all the php files that use 'itemId'
Here's my code if it helps:
$addMainItem = "INSERT INTO newsItems (itemId, title, date, tags, location, latitude, longitude, visibleFrontpage, introText, fullDome, liveEvent, customServing, visitorAttraction, retail, digitalCinema, visiblePublic, thumbPath, links, smallDesc) VALUES ('','$title','$date','$tags','$loco','$lat','$long','$visiFront','$intro','$dome ','$live','$custom','$attrac','$retail','$cinema','$public','$thumbPath','$links','$smallDesc')";
$result = mysql_query($addMainItem) or die('error '.mysql_error());
if($result) echo (mysql_insert_id());
I've never heard that naming a column itemId breaking mysql_isert_id().
But you can just select the last inserted record if auto_increment is working.
SELECT * FROM newsItems ORDER BY itemId DESC LIMIT 1
You can put the select statement into a transaction with the insert statement if you're using innoDB and you're worried about a race condition.
mysql_query("SELECT LAST_INSERT_ID()");
Isn't it what are you looking for?

Categories