Sql – Insert value in first empty cell of column and show ID - php

I have an sql statement that will insert a value to the first empty cell. And if I ran the php script again, then it inserts into the next null cell etc.
Problem: I also want to find out the ID of that row, and value of another column in that row. In the Mysql table below, I want a value inserted in the first ‘null’ of COLUMN A, and also know the id and value in COLUMN B corresponding to that (ie id=3 and COLUMN B= 11).
My_TABLE
+---------+--------------+-------------+
| ID | COLUMN A | COLUMN B |
+---------+--------------+-------------+
| 1 | 6 | 78 |
| 2 | 7 | 90 |
| 3 | NULL | 11 |
| 4 | NULL | 5 |
| 5 | NULL | 123 |
+---------+--------------+-------------+
The following sql statement in PHP script will make it possible to insert value to the first empty cell in COLUMN A:
UPDATE My_TABLE
SET COLUMN A = 83
WHERE COLUMN A IS NULL
LIMIT 1;
Result will be:
+----+----------+------------+
| ID | COLUMN A | COLUMN B |
+----+----------+------------+
| 1 | 6 | 78 |
| 2 | 7 | 90 |
| 3 | 83 | 11 |
| 4 | NULL | 5 |
| 5 | NULL | 123 |
+----+----------+------------+
I also want to have an sql script that will print within PHP (echo) the values of ID and COLUMN B values corresponding to the first COLUMN A null value (ie ID= 3; COLUMN B= 11).

fetch the row by condition in this case you will have ID and COLUMN B
select *
from My_TABLE
where COLUMN A IS NULL
order by id
limit 1
Update by ID the selected row:
update My_TABLE
set COLUMN A = :SOME_VALUE
where ID = :ID_FROM_FETCH

Not sure if this case will fit what you are questioning.
mysqli_insert_id
From this function I'm pretty sure you will able to write the script for what you need.
Note* If it fits what you need, please read the warning as I'm not
sure if it will deprecated from the latest PHP version. Kindly take
note.

Related

How to update table columns that are stored as rows?

I am first time trying to update plugin data in wordpress database. I am using Advanced CF7 DB plugin for storing contact form 7 data. For updating the column I dont know whether pivot is required or there is anyother way to achieve this. Have searched but found two tables using pivot. I want to update data in same table just the columns are in rows. Dont know how to do this. Data is stored in below format:
| id | cf7_id | data_id | name | value
+----+--------+---------+-----------------+------------------
| 9 | 4561 | 2 | uid | 60eabb2c20021
| 10 | 4561 | 2 | emailId | axxxxx#gmail.com
| 11 | 4561 | 2 | clicked_on_link | No
| 12 | 4561 | 2 | orderNos | Miscommunication
| 13 | 4561 | 2 | submission_id | 177
+----+--------+---------+-----------------+------------------
I want to update column clicked_on_link= Yes where uid= 60eabb2c20021. uid will be passed from url parameter.
| 11 | 4561 | 2 | clicked_on_link | Yes
I want this query in php-sql form so that I can include this in code.I have tried following query which I know is wrong but tried atleast:
update wp_cf7_vdata_entry as b1,
(select data_id,MAX(CASE WHEN name='uid' THEN value ELSE NULL END) AS
uid,
MAX(CASE WHEN name='email_id' THEN value ELSE NULL END) AS email_id,
MAX(CASE WHEN name='clicked_on_link' THEN value ELSE NULL END) AS
clicked_on_link
from wp_cf7_vdata_entry group by data_id) as b2
set b2.clicked_on_link='Yes'
where b1.data_id=b2.data_id
and b2.uid=60ea1e95190ee;
You can use join:
update wp_cf7_vdata_entry e join
wp_cf7_vdata_entry e2
on e.data_id = e2.data_id
set e2.value = 'yes'
where e.name = 'uid' and e.value = '60eabb2c20021' and
e2.name = 'clicked_on_link';
Note: I'm not sure if cf7_id is relevant for the join condition, but your code only uses data_id.

Updating the table with new value by finding specific row

I have a table that I add information depending on the order number.
So when I enter information, I add some column names as numbers.
Then update the rows with new values.
My table takes 3 values everytime I insert value.
order number| total left | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|
------------------------------------------------------------------------
11 | 100 | a | b | c | d | e | f | 0 | | |
------------------------------------------------------------------------
12 | 10 | x | y | z | 0 | | | s | d | f|
-------------------------------------------------------------------------
When I try to add new column, the value of the new rows in every other value becomes null or 0 depending on if it is int or varchar.
When I insert 3 values to order number 12, I want the last 0 in that row to be updated.
(In this case, 4-5-6 but I get 7-8-9 updated.)
So what is the best way for finding the last row which is 0
(in this case column 4 for order number 12 and insert the new values of s,d,f to the 4-5-6 instead of 7-8-9 ?
So maybe something like:
Loop through rows, find 0, insert 3 rows, break.
I take the last column:
$NewColumnNameKoliAdet=$LastColumnName+1;
$NewColumnNameMusteri=$LastColumnName+2;
$NewColumnNameTarih=$LastColumnName+3;
Then I add columns and update the table.
$Query="ALTER TABLE `koli_stok_hareketleri`
ADD `$NewColumnNameKoliAdet` INT(11) NOT NULL AFTER `$LastColumnName`,
ADD `$NewColumnNameMusteri` VARCHAR(100) NOT NULL AFTER `$NewColumnNameKoliAdet`,
ADD `$NewColumnNameTarih` VARCHAR(100) NOT NULL AFTER `$NewColumnNameMusteri`;";
$Query="UPDATE koli_stok_hareketleri SET kalan_koli=kalan_koli-
'$Uretilen_Koli',
`$NewColumnNameKoliAdet` = '$Uretilen_Koli',
`$NewColumnNameMusteri` = '$Musteri_ismifromrow',
`$NewColumnNameTarih` = '$Now'
WHERE koli_ismi ='$Koli_IsmifromRow' AND
koli_parti_no='$Parti_NofromRow'";
So the problem is it adds three value to each row automatically and but when I update, I need to update the order number 12 from 4th column not 7.

PHP MySQL consolidate column where other column has duplicates

I have a MySQL table that has three columns, the first is a unique key (INT(11) AUTO_INCREMENT), the next is an indexed value (VARCHAR(255)) and the third is a description (TEXT). There are duplicate values in the second column, but each row has a different description. I want to remove all rows where the second column is duplicated but append each description of the same indexed value to the first instance the value, and breaking string with a semicolon and space.
For example, my table looks like this:
cid | word | description
------------------------------
1 | cat | an animal with wiskers
2 | cat | a house pet
3 | dog | a member of the canine family
4 | cat | a cool person
I want to change the table to look like this:
cid | word | description
------------------------------
1 | cat | an animal with wiskers; a house pet; a cool person
3 | dog | a member of the canine family
I'm not adverse to using a PHP script to do this, but would prefer MySQL. The table has over 170,000 rows and would take PHP a long time to loop over it.
SQL:
select `cid`,`word`,group_concat(`description` SEPARATOR '; ') as `description` from `test_table` group by `word`;
Ok.. you can copy all the data into another table, and rename it then..
insert into `test_new` (`cid`,`word`,`desc`) (select `cid`,`word`,group_concat(`desc` SEPARATOR '; ') as `description` from `test_table` group by `word`);
mysql> describe `test_new`;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| word | char(10) | YES | | NULL | |
| desc | text | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from `test_new`;
+------+------+---------------------+
| id | word | desc |
+------+------+---------------------+
| 1 | cat | desc1; desc2; desc4 |
| 3 | dog | desc3 |
+------+------+---------------------+
2 rows in set (0.00 sec)
As was mentioned before, you can create a new table and copy the info, you can also do it in two steps, but only if there´s no problem with modifying the old table:
UPDATE tableOld AS N1, tableOld AS N2
SET N1.description = concat(concat(N1.description,"; "),N2.decription))
WHERE N2.word = N1.word
insert into tableNew (cid,name,description)select * from tableOld group by word

How to display the previous row and next row from varchar value

I have hundreds rows of data. I want to get the previous row and the next row from the current item. I saw many examples here but none solve my problem.
My data key is a set of string (md5) which is unable to compare the greater or lower like the other does. Here's my resources.
---------------------------------------------------
| id| sid | name |
----+----------------------------------+----------|
| 1 | c81e728d9d4c2f636f067f89cc14862c + Mr.A |
----+----------------------------------+----------|
| 2 | eccbc87e4b5ce2fe28308fd9f2a7baf3 | Mr.B |
----+----------------------------------+----------|
| 3 | a87ff679a2f3e71d9181a67b7542122c | Mr.C |<current position
----+----------------------------------+----------|
| 4 | e4da3b7fbbce2345d7772b0674a318d5 | Mr.D |
----+----------------------------------+----------|
| 5 | 1679091c5a880faf6fb5e6087eb1b2dc | Mr.E |
--------------------------------------------------|
So, is there any way to get the previous row (Mr.B) and the next row (Mr.D) with mysql?
I've tried
SELECT * FROM table
WHERE sid < #sid
ORDER BY sid DESC
LIMIT 1
but it's not work because sid is uncomparable.

INSERT statement in joined table

I have 3 table:
tblNames:
| id | firstname | lastname |
+------+---------------+--------------+
| 1 | John | Smith |
tblJosbs (this table accepts multiple checkbox value at the same time):
| id | jobs |
+------+-----------------------+
| 1 | Nurse |
+------+-----------------------+
| 2 | Call Center Agent |
+------+-----------------------+
| 3 | Police |
tblNamesJobs (this table is used to JOIN the other 2 tables):
| id | name_id | jobs_id |
+------+-------------+-------------+
| 1 | 1 | 1 |
+------+-------------+-------------+
| 2 | 1 | 2 |
+------+-------------+-------------+
| 3 | 1 | 3 |
All is fine but can someone show me the INSERT statement for the 3rd table I should use to when I will add new information?
For example add record that John Smith is a Call Center Agent
insert into tblNamesJobs (name_id,jobs_id )
values (
select id from tblNames where
firstname='John'
and lastname='Smith' limit 1
,
select id from tblJosbs where jobs='Call Center Agent' limit 1
);
If you are already depending on tauto increment..you can get the lastinserid, depending on your adapter.
eg. mysql_insert_id
for PDO we can use --PDO::lastInsertId.
so you will have id's of earlier inserted tables, that you can save in the new one.
INSERT INTO tblNamesJobs (name_id, jobs_id) VALUES (XXXX,YYYY)
That is assuming the table's id is auto-incrementing.
It should be noted that both the name_id and jobs_id columns in the "joiner" table should be foreign keys to the respective columns in the other table.
Edit - Valex's answer goes into more detail about what to do if you don't already have the id values.
If possible, I would recommend using some sort of framework that would handle the "joiner" table for you.

Categories