Mysql update a row with another row value in same table - php

I have a table. I want to update the 5th row with 10th row values from the same table. For example:
SlNo Name Quali Exp
1 x B.E 2
2 y BSC 3
3 Z B.A 1.5
4 A MSC 2
5 B MBA 5
Here i want to update second row with the value of 5th row.
Here is my current query:
UPDATE table
SET Name=(select Name from table where slNo='5'),
Quali=(select Quali from table where slNo='5'),
Exp=(select Exp from table where slNo='5')
where slNo='3';
this is working fine ... but if there are more than 20 columns it becomes laborious to write a query this way, because for each column I have to include another sub-query... is there any other way to write query to update the whole row with all values from the other row in the same table?

Use a self-join with the multiple table UPDATE syntax:
UPDATE `table` AS t1 JOIN `table` AS t2 ON t2.slNo = 5
SET t1.Name = t2.Name, t1.Quali = t2.Quali, t1.Exp = t2.Exp
WHERE t1.slNo = 3

Related

how to increment 1 every row after get row id in sql

i have a one question for increment +1 in every row
i have a sql table in that table have order_status column when new record created order_status will be store +1 then older record in that case if i have order status like 0 1 2 3
but when user send me order status 2 for 5th record so in this case record no 5 will be store as a record status 2 but i need to change status aftr 2 like
2 will be 3, 3 will be 4, 4 will be 5
if its posible so please me sql query
Use an UPDATE query.
UPDATE yourTable
SET order_status = order_status + 1
WHERE order_status >= 2

add two sql queries

How do I add two sql queries and result the final value of the second column ?
Example?
TABLE ONE
colum 1 | colum 2
10 | 5
RESULT:
TABLE ONE
colum 1 | colum 2
0 | 15
Query:
select colum 1 + colum 2 from TABLE ONE
Are you looking to update the rows with the squashed values?
UPDATE Table1 t1 SET t1.column2 = t1.column1 + t1.colum2, t1.column1 = 0;

update rows with same column value mysql and delete one

TABLE cartitems
CartItemId|CartId|sku_id|prod_id|quantity
1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1
2 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1
I want to merger the rows having same sku_id and update the quantity and delete one duplicate row like this
CartItemId|CartId|sku_id|prod_id|quantity
1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 1&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 2
if delete doesnt happens its okay. i l manage it later.
it's simple Aggregate function with an ORDER BY Keyword to
use.
try,
select
min(CartItemId),
min(CartId),
sku_id,
Sum(quantity) from cartitems group by sku_id

How do i update 1 field or 2 of a database table if another table has matching field values?

Let's say I have a table with these columns and values:
id field
-----------
1 0001
2 0002
3 0003
4 0004
5 0005
Then i have this other table with these fields and values:
id tid info_1 field_1 info_2 field_2
---------------------------------------------------
1 1 7 0000 null null
2 2 null null 10 0002
3 3 17 0003 null null
4 4 29 1111 null null
5 6 null null 44 0005
I need an update query that will update the records of table 2, just in the info columns, but only if table 1 has matching fields in table 2 and also only when id of first table and tid of second table match.
So for example table 1 id of 1 and table 2 tid of 1 match but field_1 or field_2 doesn't have the same field number as table 1.
Table 1 id of 2 and table 2 tid of 2 match and field_2 does have a matching number as the field in table one, so i would like to change info_2 of record 2 of table 2 to a number of my choosing. let's just say 50. in all cases the number in info_1 or info_2 will be changed to 50.
Now table 1 id of 3 and table 2 tid of 3 match and field_1 has matching number as the field in table 1, so i need to change info_1 to 50 for that 3rd record in table 2.
In record 4 the id and tid match but no fields match so skip that one.
In record 5 even though field_2 of table 2 matches field in table one, the id of first table and tid of second table do not match, so that 5th record can be skipped.
Here is what i tried so far:
$updatequery1 = "UPDATE table2 LEFT JOIN table1 a ON a.field = field_1 SET info_1 = 50";
$updateresult1 = mysql_query($updatequery1) or die(mysql_error());
So for this first attempt i didn't set id and tid to match, i just went right for looking for field matching. i also didn't incorporate field_2. not even sure if it can all be done in one pass or if multiple queries are needed. in any case, this attempt made all info_1 values equal to 50 even if there were no field matches.
Can someone help me with this query and make both info columns change at same time if possible?
If i were to do a select query for selecting all records that match in table 1 id and table 2 tid, it would be like this, but don't know how to incorporate it into the update query.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
I can also try and incorporate field matching and it would probably be something like this:
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tid
WHERE table1.field = table2.field_1
OR table1.field = table2.field_2
But again, wouldn't know how to turn that into an update query so that either info_1 or info_2 changes accordingly.
So to summarize in a quicker way. if id and tid match and field and field_1 match, info_1 should change to 50. if id and tid match and field and field_2 match, info_2 should change to 50
you can use CASE statement in the SET
UPDATE Table2 as T2
JOIN Table1 T1
ON T2.tid = T1.id
set
info_1 = CASE WHEN T2.field_1 = T1.field then 50 ELSE T2.field_1 END ,
info_2 = CASE WHEN T2.field_2 = T1.field then 50 ELSE T2.field_2 END

php pdo copy column between tables

I got the two tables(Table1 and Table2):
Table1:
id hits url
1 11 a
2 5 b
3 6 c
4 99 d
5 14 e
Table2:
id url 2014.04.13 2014.04.14
1 a 0 5
2 b 0 1
3 c 0 3
4 d 0 60
5 e 0 10
hi all,
Table1 one contains the actual hits(which are always up-to-date) and Table2 to statistics(which are done every day at midnight). The columns id(unique number) and url are in both tables the same. So they got the same amount of rows.
So i create every day a new column(with the date of today) and copy the column hits from the table 'Table1' into the new created column into the table 'Table2'
First i alter Table2:
$st = $pdo->prepare("ALTER TABLE Table2 ADD `$today_date` INT(4) NOT NULL");
$st->execute();
Then i cache all entries i need from Table1:
$c = 0;
$id = array();
$hits = array();
$sql = "SELECT id, hits FROM Table1 ORDER BY id ASC";
$stmt = $pdo->query($sql);
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id[$c] = $row['id'];
$hits[$c] = $row['hits'];
$c++;
}
At last i update Table2:
for ($d = 0 ; $d < $c ; $d++)
{
$id_insert = $id[$d];
$sql = "UPDATE DOWNLOADS_TEST SET `$datum_det_dwnloads`=? WHERE id=?";
$q = $pdo->prepare($sql);
$q->execute(array($hits[$d], $id[$d]));
if($q->rowCount() == 1 or $hits[$d] == 0) // success
$hits[$d] = 0;
else // error inserting (e.g. index not found)
$d_error = 1; // error :( //
}
So what i need is to copy(insert) a column from one table to another.
The two tables are having ~2000 elements and the copying as described above takes around 40 sec. The bottleneck is the last part (inserting into the Table2) as i found out.
One thing i found is to do multiple updates in one query. Is there anything i can do besides that?
I hope you realise that at some point your table will have irrational number of columns and will be highly inefficent. I strongly advise you to use other solution, for example another table that holds data for each row for each day.
Let's say you have a table with 2000 rows and two columns: ID and URL. Now you want to know the count of hits for each URL so you add column HITS. But then you realise you will need to know the count of hits for each URL for every date, so your best bet is to split the tables. At this moment you have one table:
Table A (A_ID, URL, HITS)
Now remove HITS from Table A and create Table B with ID and HITS attributes). Now you have:
Table A (A_ID, URL)
Table B (B_ID, HITS)
Next move is to connect those two tables:
Table A (A_ID, URL)
Table B (B_ID, A_ID, HITS)
Where A_ID is foreign key to attribute "A_ID" of Table A. In the end it's the same as first step. But now it's easy to add date attribute to Table B:
Table A (A_ID, URL)
Table B (B_ID, A_ID, HITS, DATE)
And you have your solution for database structure. You will have a lot of entries in table B, but it's still better than a lot of columns. Example of how it would look like:
Table A | A_ID | URL
0 index
1 contact
Table B | B_ID | A_ID | HITS | DATE
0 0 23 12.04.2013
1 1 12 12.04.2013
2 0 219 13.04.2013
3 1 99 13.04.2013
You can also make unique index of A_ID and DATE in Table B, but I prefer to work on IDs even on linking tables.

Categories