How I can add values from my table without using update? - php

How I can add values to my mysql table with php without using update and only using select?
My table have a row with the name sale. How I can do this with php?
Thanks.
#EDIT:
One guy give me this but isn't working for me:
SELECT saldo + 50 as saldo FROM tabela;

Some code would be helpful, but you cannot add values into your MySQL table with using UPDATE, UPDATE is to UPDATE values.
UPDATE tbl SET sale = 10000 WHERE tbl_id=1;
To insert/add values, you have to use INSERT:
INSERT INTO tbl (sale) VALUES (10000);
You can use SELECT only if you are for example, pulling data from one table into your current one:
INSERT INTO tbl (sale) SELECT (sale_scores) FROM tbl2;
Or another way is to use SELECT INTO
SELECT * INTO tbl3 FROM tbl;

You can use REPLACE which has the same syntax as INSERT, but replaces the existing row at the coincidence of a unique key.
Example:
Table before:
| id | name |
| -- | ---- |
| 1 | Nick |
| 2 | John |
Run: REPLACE table_name (id, name) VALUES (2, "Sam")
Table after:
| id | name |
| -- | ---- |
| 1 | Nick |
| 2 | Sam |

Personally, I'd use PDO with a simple UPDATE request and bind the params. I see no point in trying to get around using something that is already in place and works a treat.
Here's an example of what I'd do.
$stmt = $this->conn->prepare("UPDATE `table` SET `saldo` = `saldo` + 50 WHERE `username` = :uname");
$stmt->bindParam(":uname",$username);
$stmt->execute();

Related

search comma separated values in mysql using php

I am having a bit of a problem running a select query on a database. Some of the data is held as a list of comma separated values, an example:
Table: example_tbl
| Id | standardid | subjectid |
| 1 | 1,2,3 | 8,10,3 |
| 2 | 7,6,12 | 18,19,2 |
| 3 | 10,11,12 | 4,3,7 |
And an example of the kind of thing I am trying to run:
select * from table where standardid in (7,10) and subjectid in (2,3,4)
select * from table where FIND_IN_SET(7,10,standardid) and FIND_IN_SET(2,3,4,subjectid)
Thanks in advance for anything you can tell me.
comma separated values in a database are inherently problematic and inefficient, and it is far, far better to normalise your database design; but if you check the syntax for FIND_IN_SET() it looks for a single value in the set, not matches several values in the set.
To use it for multiple values, you need to use the function several times:
select * from table
where (FIND_IN_SET(7,standardid)
OR FIND_IN_SET(10,standardid))
and (FIND_IN_SET(2,subjectid)
OR FIND_IN_SET(3,subjectid)
OR FIND_IN_SET(4,subjectid))

MySQL Insert and Update Trigger on Concatenated Column not working

I have the following MySQL table named users
+----------------+----------------+----------+ +----------+
| uniquenum | state | type | | custid |
+----------------+----------------+----------+ +----------+
+----------------+----------------+----------+ +----------+
| 00001 | 03 | 1 | | 10300001 |
+----------------+----------------+----------+ +----------+
| 00002 | 02 | 3 | | 30200002 |
+----------------+----------------+----------+ +----------+
The above three columns uniquenum, state and type are all concatenated and shown in the custid column. This I was able to achieve by running the following SQL query:
UPDATE users SET custid = concat (type,state,uniquenum)
What I am trying to achieve is to make the custid column to automatically get the values of the other three columns when new values are added or the old ones updated. So, I tried to create a trigger as follows:
CREATE trigger insert_custid
before insert on users
for each row
set new.custid = concat(new.type,new.state,new.uniquenum);
create trigger update_custid
before update on users
for each row
set new.custid = concat(new.type,new.state,new.uniquenum);
When I do this and insert into the table, the custid column does not store the correct values. Instead it returns all zeros in place of the value of uniquenum.
uniquenum is AUTO_INCREMENT with zerofill and starts at 00001 with an interval of 1. Could this be causing an issue? Any help is greatly appreciated. Thank you :)

How to remove duplicate row considering the Arabic Phonetics

I have a table of Arabic text. I want to remove duplicate rows. In view of the symbols in Arabic language: َ ِ ُ
My table: vocabulary
+----+----------+--------------------------------+
| id | word | mean |
--------------------------------------------------
| 1 | سِلام | xxx |
--------------------------------------------------
| 2 | سَلام | xxx |
--------------------------------------------------
| 3 | سلام | xxx |
--------------------------------------------------
| 4 | سلام | xxx |
+------------------------------------------------+
Now i want this table:
+----+----------+--------------------------------+
| id | word | mean |
--------------------------------------------------
| 1 | سِلام | xxx |
--------------------------------------------------
| 2 | سَلام | xxx |
--------------------------------------------------
| 3 | سلام | xxx |
+------------------------------------------------+
How can i do that ?!
My Try:
$result = mysql_query( "SELECT * FROM vocabulary where");
while($end = mysql_fetch_assoc($result)){
$word = $end["word"];
$mean = $end["mean"];
$id = $end["id"];
$result2 = mysql_query( "SELECT * FROM vocabulary where word='$word' AND mean='$mean'");
$TotalResults = mysql_num_rows($result2);
if($TotalResults>1){
mysql_query( "DELETE FROM vocabulary WHERE id='$id'");
}
Summary: How can I sensitive MySQL to the Arabic symbols ?
There are multiple ways to achieve this.
1- You can either select your rows from the database, loop through them and save the 'word' title in an array, and in each iteration in the loop, you can check if a similar value is in_array(). If the value exists, then you can save the id in another array and then use these ids to delete from the database.
2- Another way to extract the ids is to use a query similar to the below:
select count(*), id from table group by title
You can then loop through the results and delete the row (using the ids) where count is greater than 1.
The basic concept in both (and other methods) is that you just have to match the strings. Phonetics on letters change the actual string so "سَلام" is not equal to "سلام".
On a side note, there is a great Arabic PHP library you can use for various Arabic related string manipulation: PHP and Arabic Language.
This way will only remove one duplicate.
There are several other ways to do it, and it all depends on the size of the data set you have and if deleting these duplicates is a one time thing or a frequent thing because you will have to keep performance in mind.
I haven't tested it, but this should work:
CREATE TEMPORARY TABLE tmp_keeps
SELECT title, MIN(id) AS keepID
FROM theTable
GROUP BY title
;
DELETE FROM theTable
WHERE (title, id) NOT IN (
SELECT title, keepID
FROM tmp_keeps
)
;
DROP TEMPORARY TABLE tmp_keeps;
It (in the subquery) gets the first id for each title, and then deletes rows that don't meet that condition.
Edit: Revised to avoid SQL error pointed out in comments.
If it is a large table, something along the lines of Adon's answer might be faster.

How to track a secondary index id

Have a table that will be shared by multiple users. The basic table structure will be:
unique_id | user_id | users_index_id | data_1 | data_2 etc etc
With the id fields being type int and unique_id being an primary key with auto increment.
The data will be something like:
unique_id | user_id | users_index_id
1 | 234 | 1
2 | 234 | 2
3 | 234 | 3
4 | 234 | 4
5 | 732 | 1
6 | 732 | 2
7 | 234 | 5
8 | 732 | 3
How do I keep track of 'users_index_id' so that it 'auto increments' specifically for a user_id ?
Any help would be greatly appreciated. As I've searched for an answer but am not sure I'm using the correct terminology to find what I need.
The only way to do this consistently is by using a "before insert" and "before update" trigger. MySQL does not directly support this syntax. You could wrap all changes to the table in a stored procedure and put the logic there, or use very careful logic when doing an insert:
insert into table(user_id, users_index_id)
select user_id, count(*) + 1
from table
where user_id = param_user_id;
However, this won't keep things in order if you do delete or some updates.
You might find it more convenient to calculate the users_index_id when you query rather than in the database. You can do this using either subqueries (which are probably ok with the right indexes on the table) or using variables (which might be faster but can't be put into a view).
If you have an index on table(user_id, unique_id), then the following query should work pretty well:
select t.*,
(select count(*) from table t2 where t2.user_id = t.user_id and t2.unique_id <= t.unique_id
) as users_index_id
from table t;
You will need the index for non-abyssmal performance.
You need to find the MAX(users_index_id) and increment it by one. To avoid having to manually lock the table to ensure a unique key you will want to perform the SELECT within your INSERT statement. However, MySQL does not allow you to reference the target table when performing an INSERT or UPDATE statement unless it's wrapped in a subquery:
INSERT INTO users (user_id, users_index_id) VALUES (234, (SELECT IFNULL(id, 0) + 1 FROM (SELECT MAX(users_index_id) id FROM users WHERE user_id = 234) dt))
Query without subselect (thanks Gordon Linoff):
INSERT INTO users (user_id, users_index_id) SELECT 234, IFNULL((SELECT MAX(users_index_id) id FROM users WHERE user_id = 234), 0) + 1;
http://sqlfiddle.com/#!2/eaea9a/1/0

WHERE vs HAVING in generated queries

I know that this title is overused, but it seems that my kind of question is not answered yet.
So, the problem is like this:
I have a table structure made of four tables (tables, rows, cols, values) that I use to recreate the behavior of the information_schema (in a way).
In php I am generating queries to retrieve the data, and the result would still look like a normal table:
SELECT
(SELECT value FROM `values` WHERE `col` = "3" and row = rows.id) as "col1",
(SELECT value FROM `values` WHERE `col` = "4" and row = rows.id) as "col2"
FROM rows WHERE `table` = (SELECT id FROM tables WHERE name = 'table1')
HAVING (col2 LIKE "%4%")
OR
SELECT * FROM
(SELECT
(SELECT value FROM `values` WHERE `col` = "3" and row = rows.id) as "col1",
(SELECT value FROM `values` WHERE `col` = "4" and row = rows.id) as "col2"
FROM rows WHERE `table` = (SELECT id FROM tables WHERE name = 'table1')) d
WHERE col2 LIKE "%4%"
note that the part where I define the columns of the result is generated by a php script. It is less important why I am doing this, but I want to extend this algorithm that generates the queries for a broader use.
And we got to the core problem, I have to decide if I will generate a where or a having part for the query, and I know when to use them both, the problem is my algorithm doesn't and I have to make a few extra checks for this. But the two above queries are equivalent, I can always put any query in a sub-query, give it an alias, and use where on the new derived table. But I wonder if I will have problems with the performance or not, or if this will turn back on me in an unexpected way.
I know how they both work, and how where is supposed to be faster, but this is why I came here to ask. Hopefully I made myself understood, please excuse my english and the long useless turns of phrases, and all.
EDIT 1
I already know the difference between the two, and all that implies, my only dilemma is that using custom columns from other tables, with variable numbers and size, and trying to achieve the same result as using a normally created table implies that I must use HAVING for filtering the derived tables columns, at the same time having the option to wrap it up in a subquery and use where normally, this probably will create a temporary table that will be filtered afterwards. Will this affect performance for a large database? And unfortunately I cannot test this right now, as I do not afford to fill the database with over 1 billion entries (that will be something like this: 1 billion in rows table, 5 billions in values table, as every row have 5 columns, 5 rows in cols table and 1 row in tables table = 6,000,006 entries in total)
right now my database looks like this:
+----+--------+-----------+------+
| id | name | title | dets |
+----+--------+-----------+------+
| 1 | table1 | Table One | |
+----+--------+-----------+------+
+----+-------+------+
| id | table | name |
+----+-------+------+
| 3 | 1 | col1 |
| 4 | 1 | col2 |
+----+-------+------+
where `table` is a foreign key from table `tables`
+----+-------+-------+
| id | table | extra |
+----+-------+-------+
| 1 | 1 | |
| 2 | 1 | |
+----+-------+-------+
where `table` is a foreign key from table `tables`
+----+-----+-----+----------+
| id | row | col | value |
+----+-----+-----+----------+
| 1 | 1 | 3 | 13 |
| 2 | 1 | 4 | 14 |
| 6 | 2 | 4 | 24 |
| 9 | 2 | 3 | asdfghjk |
+----+-----+-----+----------+
where `row` is a foreign key from table `rows`
where `col` is a foreign key from table `cols`
EDIT 2
The conditions are there just for demonstration purposes!
EDIT 3
For only two rows, it seems there is a difference between the two, the one using having is 0,0008 and the one using where is 0.0014-0.0019. I wonder if this will affect performance for large numbers of rows and columns
EDIT 4
The result of the two queries is identical, and that is:
+----------+------+
| col1 | col2 |
+----------+------+
| 13 | 14 |
| asdfghjk | 24 |
+----------+------+
HAVING is specifically for GROUP BY, WHERE is to provide conditional parameters. See also WHERE vs HAVING
I believe the having clause would be faster in this case, as you're defining specific values, as opposed to reading through the values and looking for a match.
See: http://database-programmer.blogspot.com/2008/04/group-by-having-sum-avg-and-count.html
Basically, WHERE filters out columns before passing them to an aggregate function, but HAVING filters the aggregate function's results.
you could do it like that
WHERE col2 In (14,24)
your code WHERE col2 LIKE "%4%" is bad idea so what about col2 = 34 it will be also selected.

Categories