Updating mySQL field where there are multiple matches - mysql

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');

Related

Issues when getting ID from DB [duplicate]

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.

why ami getting this #1305 - FUNCTION homeshopping.partID does not exist

mysql php admin table query enter image description here
I don't see why it says partID is giving me a problem? it is in the table and i think i have them linked correctly. I Have changed a few things i replaced comma with the and statement which cleared up alot of my errors. But since I've done that it continues to give me #1305 - FUNCTION homeshopping.partID does not exist. I have even looked at the structures an designer to make sure column names an tables are correct.
Don't forget to post your query as text also.
Because it's missing the keywork IN to have a query like this:
SELECT CONCAT(hscust.first,' ', hscust.last AS Customer,hsitems.description,hsitems.price,hsitems.itemCalss
FROM hscust,hsorders,hslineitem,hsitems
WHERE hslineitems.orderId = hsorders.orderId AND hsitems.partID = hslineitem.partNum AND hslineitem.price = hsitems.price AND partID IN ('CB03', 'CZ82');

Updating/changing a specific portion of a string in mysql?

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%';

Why is my query wrong?

before i use alias for table i get the error:
: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
Then i used aliases and i get this error:
unknown index a
I am trying to get a list of category name ( dependant to a translation) and the associated category id which is unique. Since i need to put them in a select, i see that i should use the lists.
$categorie= DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->lists('nome.nome','a.id');
The best way to debug your query is to look at the raw query Laravel generates and trying to run this raw query in your favorite SQL tool (Navicat, MySQL cli tool...), so you can dump it to log using:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
});
Doing that with yours I could see at least one problem:
->where('lingua.lingua','=','it-IT')
Must be changed to
->where('lingua.lingua','=',"'it-IT'")
As #jmail said, you didn't really describe the problem very well, just what you ended up doing to get around (part of) it. However, if I read your question right you're saying that originally you did it without all the aliases you got the 'ambiguous' error.
So let me explain that first: this would happen, because there are many parts of that query that use id rather than a qualified table`.`id.
if you think about it, without aliases you query looks a bit like this: SELECT * FROM `cat` JOIN `campo_cat` ON `id_cat` = `id` JOIN `campo` ON `id` = `id_campo`; and suddenly, MySQL doesn't know to which table all these id columns refer. So to get around that all you need to do is namespace your fields (i.e. use ... JOIN `campo` ON `campo`.`id` = `campo_cat`.`id_campo`...). In your case you've gone one step further and aliased your tables. This certianly makes the query a little simpler, though you don't need to actually do it.
So on to your next issue - this will be a Laravel error. And presumably happening because your key column from lists($valueColumn, $keyColumn) isn't found in the results. This is because you're referring to the cat.id column (okay in your aliased case a.id) in part of the code that's no longer in MySQL - the lists() method is actually run in PHP after Laravel gets the results from the database. As such, there's no such column called a.id. It's likely it'll be called id, but because you don't request it specifically, you may find that the ambiguous issue is back. My suggestion would be to select it specifically and alias the column. Try something like the below:
$categories = DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->select('nome.nome as nome_nome','a.id as a_id') // here we alias `.id as a_id
->lists('nome_nome','a_id'); // here we refer to the actual columns
It may not work perfectly (I don't use ->select() so don't know whether you pass an array or multiple parameters, also you may need DB::raw() wrapping each one in order to do the aliasing) but hopefully you get my meaning and can get it working.

Hyphen within string in mysql query causing strange behavior

In my Rails testing environment, I have a user_id that looks like 1234-567abc89. I'm getting inconsistent behaviour by querying this user in different tables. Most of the queries are working, but running one particular query fails:
ActiveRecord::StatementInvalid (Mysql::Error: Unknown column '1234' in
'where clause': SELECT * FROM `point_allocations` WHERE (user_id = 1234-567abc89) ):
So for some reason, everything beyond the hyphen is getting cut off. I realized that for the queries that work, it is looking up user 1234 instead of 1234-567abc89, but if all the others work, any idea why only this one would return an error?
You need to include quotations.
SELECT * FROM `point_allocations` WHERE (user_id = '1234-567abc89')
Because the user_id column expects character-typed data, it will take your value (1234-567abc89) and parse it as an integer, truncating the content after the hyphen. If you include it in quotations, it will accept it as a string and transfer properly.
Enjoy and good luck!