This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
I hope you all have a good day.
Actually I'm deploying a query to get a complete array of data. But the fact is that I need an Id, first of all, I guess, I must get the last Id first, then I can apply a mathematic operation to get its value + 1. The fact is that I've been trying different sentences and queries with no result.
This is my code:
function obtener_Id(){
global $mysqli;
$resultado_oId = $mysqli ->query("SELECT TOP 1 'id' FROM 'pacient' ORDER BY RowID DESC ")
$id_sacada = mysqli_fetch_assoc($resultado_oId);
$id_enLaMano = $id_sacada['id'];
return $id_enLaMano;
//$id_dinamica = $id_enLaMano + 1;
//return $id_dinamica;
}
As you can see guys, I've commented on the last two lines, cause I´m looking to get at least a value (The result from a query) But Idk if that is correct. Looking on the Internet I've seen relative posts which are solved just to apply the query that we can view under global declarations...
I've tried that on phpMyAdmin with no results and a bunch of errors...
You guys know the correct way to get the max value in the Id column? Or even if I'm doing badly correct me.
A lot of hugs and Luck!
Mizar ^^
I would suggest putting the serial number in a table. Read the serial no before insert, (lock the table, if required) insert the data, then increase the serial no by 1 and update the serial no table with increased value.
Related
This question already has answers here:
MySQL You are using safe update mode and you tried to update a table without a WHERE
(2 answers)
Closed 2 years ago.
I'm trying to add a numeric value to a column whose "AuthDate" is "06/05/20" and "Time" is "1P -2P".
I ran the following query:
update main
set calls = 2072
where authdate = '06/05/20'
and time = '1P - 2P';
*main = table name
calls = column name
but I get error code 1175 You are using safe update mode
Does anyone know why I get the error?
Your database or session has option sql_safe_updates enabled. Amongst other things, this forbids update that do not have a where clause, or that have a where clause whose columns are not keys (are not indexed). Even if there is an index, but the optimize chooses not to use it, the query is also aborted.
This is more of an option that prevents mistakes from beginners. If you really know what you are doing, you can disable the option at session level:
set sql_safe_updates = 0;
Side note: I am suspicious about condition authdate = '06/05/20':
if autdate is of a date-like datatype, then you should give it a legitimate date string, like: authdate = '2020-06-05' (or '2020-05-06'?)
if it's a string then... consider storing it as a date; storing dates as string is bad practice for many reasons, and should be avoided
I have tried a couple different approaches to doing this, and all seem to return an error. So in this exact situation, I'm trying to replace something in garrysmod with another. In this case, it's a playermodel. And I'm using the mysql pointshop. I want to replace specifically in the items row only the word "ironman" with "vector". Here are the different ones I have tried:
UPDATE `pointshop_data` SET `items` = REPLACE(`items`, 'ironman', 'vector')
and
UPDATE pointshop_data SET items = REPLACE(items, 'ironman', 'vector') WHERE items LIKE '%ironman%';
Both of which came from here: MySql - Way to update portion of a string?
Any different approaches I've tried I get the same syntax error: http://gyazo.com/03a6774b2d78956a8c5b41c588e9c568
I feel like I'm missing the smallest step here, but I did exactly as the answers stated in the other question.
try following query:
UPDATE 'pointshop_data' SET items = REPLACE(items, 'ironman', 'vector') WHERE items LIKE '%ironman%';
I am trying to update a field in a MySQL table of around 4,000 records where the email
addresses matches about 90 email addresses.
I have looked through past answers and tried to get it right but seem to be getting more errors.
I am using phpMyAdmin and this is what I first started off with:
UPDATE `user_table`.`eb_users` SET `pause` = 'X' WHERE `eb_users`.`email` LIKE ('test1#test1.com', 'another#another.com', 'moreemail#email.com');
The above throws the "Operand should contain 1 column(s) error" - I then tried different
variants of the above and got similar errors.
It's probably basic but am just not getting it... any help appreciated
If you know all of the email addresses you need to match then you can look for matches against a collection using IN rather than LIKE
UPDATE `user_table`.`eb_users` SET `pause` = 'X' WHERE `eb_users`.`email` IN ('test1#test1.com', 'another#another.com', 'moreemail#email.com');
hi i am executing nested "select" query in mysql .
the query is
SELECT `btitle` FROM `backlog` WHERE `bid` in (SELECT `abacklog_id` FROM `asprint` WHERE `aid`=184 )
I am not getting expected answer by the above query. If I execute:
SELECT abacklog_id FROM asprint WHERE aid=184
separately
I will get abacklog_id as 42,43,44,45;
So if again I execute:
SELECT `btitle` FROM `backlog` WHERE `bid` in(42,43,44,45)
I will get btitle as scrum1 scrum2 scrum3 msoffice
But if I combine those queries I will get only scrum1 remaining 3 atitle will not get.
You Can Try As Like Following...
SELECT `age_backlog`.`ab_title` FROM `age_backlog` LEFT JOIN `age_sprint` ON `age_backlog`.`ab_id` = `age_sprint`.`as_backlog_id` WHERE `age_sprint`.`as_id` = 184
By using this query you will get result with loop . You will be able to get all result with same by place with comma separated by using IMPLODE function ..
May it will be helpful for you... If you get any error , Please inform me...
What you did is to store comma separated values in age_sprint.as_backlog_id, right?
Your query actually becomes
SELECT `ab_title` FROM `age_backlog` WHERE `ab_id` IN ('42,43,44,45')
Note the ' in the IN() function. You don't get separate numbers, you get one string.
Now, when you do
SELECT CAST('42,43,44,45' AS SIGNED)
which basically is the implicit cast MySQL does, the result is 42. That's why you just get scrum1 as result.
You can search for dozens of answers to this problem here on SO.
You should never ever store comma separated values in a database. It violates the first normal form. In most cases databases are in third normal form or BCNF or even higher. Lower normal forms are just used in some special cases to get the most performance, usually for reporting issues. Not for actually working with data. You want 1 row for every as_backlog_id.
Again, your primary goal should be to get a better database design, not to write some crazy functions to get each comma separated number out of the field.
This is gonna be a dumb question, but I've been working with this code for years and never stopped to understand the what and why....
This is a very typical query I would copy and edit:
mysql_select_db($database_db, $db);
$query_qry_details = sprintf("Select * from table where id = %s", $KTColParam1_qry_details);
$qry_details = mysql_query($query_qry_details, $db) or die(mysql_error());
$row_qry_details = mysql_fetch_assoc($qry_details);
$totalRows_qry_details = mysql_num_rows($qry_details);
What do all these rows mean?
The first I know looks up the correct database. I have this line before each query on the page....do I need this?
The second row ($query_qry_details) is the query itself. I see that.
Rows 3 and 4 - no clue...
Row 5 is obviously a count of the number rows the query returns.
Thanks in advance as always.
EDITED
Shortly:
mysql_select_db(database_name, link_identifier) - Sets the cuurent active database on server that is associated with the specified link identifier.
sprintf - Return formatted string which acts as a query.
mysql_query or die - Sends a unique query to the database previously specified or exit from query.
mysql_fetch_assoc - Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead.
mysql_num_rows - Retrieves the number of rows from a result set.