I want to add data to a table, one column at a time. Therefore I don't want to always add a new row, as the column I'm adding to may not have much data in compared to the other columns. As a result I suppose I want to add the data to the first empty cell in the specified column without necessarily adding a new row to the whole table (as there may already be enough rows).
My table has 5 columns.
I'm currently using:
$sql=("INSERT INTO [MY_TABLE] ([MY_COLUMN]) VALUES ('[MY_DATA]')");
Please could someone assist with the correct code? I know it's basic stuff but I'm new to mySQL.
Thanks in advance.
So you can use UPDATE statement with a WHERE condition to target that specific row
UPDATE table_name SET column_name = 'whatever' WHERE column_name = '' AND id = '1'
If you want to update all rows where that column is blank than simply use
UPDATE table_name SET column_name = 'whatever' WHERE column_name = ''
no need of using id in above query
Reference on UPDATE
Related
How do I Show columns for specific fields .
SHOW COLUMNS FROM core_banking_mpesa WHERE FIELD= 'id' , FIELD ='LineNo' , FIELD ='Comments'
Your error is that WHERE column1='val', column2='val' is not valid syntax.
You could use IN() to select the fields:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD IN('id','LineNo','Comments')
Or use OR:
SHOW COLUMNS FROM core_banking_mpesa
WHERE FIELD='id' OR FIELD='LineNo' OR FIELD='Comments'
You could try just doing a SELECT query instead, requesting those three columns:
SELECT id, LineNo, Comments
FROM core_banking_mpesa;
If you already know what the columns are, it doesn't make much sense why you would want to use SHOW COLUMNS on this table.
First of all, in the database world, COLUMN and FIELD mean the same thing. So you can't "show columns for specific fields".
However if I understand correctly, you are trying to display data from a MySQL table for a specific set of columns/fields. If that is the case, you can try this. Use SELECT query this way :
select column_name
from information_schema.columns c
where table_name = 'core_banking_mpesa';
Basically, I want to update a column of numeric value, setting it to its previous value multiplied by a constant, but for an entire table worth of rows that meet the criteria, any help would be appreciated
edit: also every row has values independent from each other
edit 2: I'm looking for something like UPDATE table SET rowsColumnValue TO rowsColumnValue * constant WHERE differentColumnusedAsIdentifier = someNumber but that doesn't set every row to the same number, but to its previous value in that column multiplied by the given constant
edit 3: basically I want that if in a row a column value is 2 and in another one is 4, now they become 4 and 8
Please try this and let me know if it helps:
Update table_name set column_name = column_name * constant
[where column = condition]
IDEA:
Having a table of item, user, assign now if I assign one item to user which the record will be save on table of assign,
table_item:
ID------INT
NAME----TEXT
COUNT---INT
table_user:
ID-------INT
NAME-----TEXT
table_assing:
ID------INT
USER----INT (user id)
ITEM----INT (item_id)
COUNT---INT (this is for subtractions from the column of COUNT table of item)
Here I want to set trigger on inserting to table (table_assing) the value of column COUNT should subtract from column of COUNT table of table_item
This is possible on PHP that I can set to query on once action but it will take lots of code if it's possible on MySQL that will be much better and fast and effective with accuracy
simple trigger after insert on table table_assign
UPDATE table_item
SET table_item.count = (table_item.count - NEW.table_assign.count)
WHERE table_item.id = table_assign.item
Something like this should work.
DELIMITER $$
USE database_name$$
CREATE TRIGGER trigger_name AFTER INSERT ON table_asign FOR EACH ROW
BEGIN
UPDATE table_item SET count=count+NEW.count WHERE id=NEW.id;
END;$$
The 'NEW.id' refers to the new row in the table 'table_asign'
I need to get next id from table (auto_increment).
I could just use SELECT * from table ORDER BY id DESC LIMIT 1;
For example I get 50. But if we delete from table two items I will get 48 but correct one
will be 51. How get correct value even we something delete from table ?
You can only use SHOW TABLE STATUS LIKE 'tablename' to fetch the auto_increment value. A simpler solution might be: SELECT MAX(id) + 1 FROM table, but this is buggy if the last entry was deleted.
show table status like 'table_name'
next id value is in 'Auto_increment' field
SHOW TABLE STATUS LIKE 'table'
The value you want is in the Auto_increment field.
Be careful about concurrency though: by the time you get around to using this value, some other client could have inserted into the table and thus your value is out of date. It's usually best to try to not need this.
SELECT LAST_INSERT_ID() + 1;
gets the last ID used in an insert in an autoincrement column + 1
I see two solutions for the next ID:
1) Select bigger value of a column with max function. Example: select max( id ) from table;
2) Using the command SHOW STATUS LIKE and get the correct index of array. Take a look: http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
Seems to me you're creating a race condition here.
Why exactly can you not insert the row you want to insert and then use LAST_INSERT_ID() to find it's ID?
I am using MySQL, I have a table that has 9 columns. One of them is the primary key.
How can I select a single row, by the primary key or column 8 or 4?
If I understand your question correctly:
SELECT * FROM tbl WHERE id = 123 OR colname4 = 'x' OR colname8 = 'y' LIMIT 1
The 'LIMIT' keyword makes sure there is only one row returned.
select *
from MyTable
where MyPrimaryKey = 123
Columns in SQL don't have a defined 'order'. Database systems generally keep track of an order for display purposes, but it doesn't make sense to ask a database to select a column by number. You need to know the column's name in order to query its contents.
The same thing goes for the primary key (which, incidentally, may not be just a single column). You have to know which column it is, and what that column is named, in order to execute a query.
If you don't know these things, or need to figure them out dynamically, then
DESCRIBE tablename;
will tell you the names of each column, and whether it is part of the primary key or not. It will return a table that you can read, like any other result.