REPLACE in SQL does not function predictably - mysql

I want to replace every instance of a particular character in a string by another, 's' & '#' in this case
So, San Jose becomes #an Jo#e
I am using the following queries:
UPDATE city SET NewName = REPLACE(Name,'s','#') WHERE(ID<5000);
UPDATE city SET NewName = REPLACE(Name,'S','#') WHERE(ID<5000);
SELECT * FROM city WHERE Name LIKE "%s%" OR "%S%";
This is what the table looks like:
ID Name CountryCode District Population NewName
4 Mazar-e-Sharif AFG Balkh 127800 Mazar-e-#harif
5 Amsterdam NLD Noord-Holland 731200 Amsterdam
15 Enschede NLD Overijssel 149544 Enschede
19 Zaanstad NLD Noord-Holland 135621 Zaanstad
20 ´s-Hertogenbosch NLD Noord-Brabant 129170 ´s-Hertogenbosch
21 Amersfoort NLD Utrecht 126270 Amersfoort
22 Maastricht NLD Limburg 122087 Maastricht
33 Willemstad ANT Curaçao 2345 Willemstad
I am using this database. I have also added a new column to the table, NewName

Do this in one step:
UPDATE city
SET NewName = REPLACE(REPLACE(Name, 's', '#'), 'S', '#')
WHERE(ID < 5000);
Actually, the first will do if the collation for the column is case-insensitive.

Try to use next approach:
UPDATE city SET NewName = REGEXP_REPLACE(Name, '/[s]/ig', '#') WHERE(ID<5000);
OR update your request
UPDATE city SET NewName = REPLACE(REPLACE(Name,'s','#'),'S','#') WHERE(ID<5000);

Related

MySQL: SQL to remove country code from phone number

In a MySQL table [customers], I have a phone column [phone] that has phone numbers in this format:
[phone] = [+ sign][country code][space][number]
The [number] part can be in any format:
1234567890
123.456.7890
123 456 7890
(123) 456 7890
etc.
But the full phone number column value [phone] always start with [+ sign][country code][space]
Example:
[phone] values are:
+1 888-888-8888
+1 888.888 (8888)
+31 104232385
+33 143375100
+31 10 423 2385
+33 1 43 37 51 00
I want to remove the country code: [+ sign][country code][space] from all phone numbers [phone]
Example:
+1 888-888-8888 = 888-888-8888
+1 888.888 (8888) = 888.888 (8888)
+31 104232385 = 104232385
+33 143375100 = 143375100
+31 10 423 2385 = 10 423 2385
+33 1 43 37 51 00 = 1 43 37 51 00
What I want is to remove all leading characters till first space in the column. Which will always be: [+ sign][country code]
Or in other words: Remove everything before the first occurrence of certain character: [space] in MySQL?
What would be the query for it?
One way of doing this would be to do a regex replacement:
UPDATE customers
SET phone = REGEXP_REPLACE(phone, '^\\+[0-9]+ ', '');
Demo
If you're using a version of MySQL earlier than 8+, then we can use the base string functions as a workaround:
UPDATE customers
SET phone = SUBSTR(phone, INSTR(phone, ' ') + 1)
WHERE phone LIKE '+%';
Demo

select case when in MYSQL

I have 2 tables
First tabel name is "consumer"
id_consumer
name
1
Roy
2
Dori
3
Rico
Second tabel name is "consumer_address"
id_consumer
address
status
1
Street Avenue
1
1
Park Hill
0
2
Highwalk Street
1
2
Albion Place
0
Condition
name from tabel "consumer"
address from "consumer_address" , but i want to get only 1 address when consumer_address.status = 1
When Consumer not have data in tabel "consumer_address", field is NULL
The Final Tabel Like this
id_consumer
name
address
status
1
Roy
Street Avenue
1
2
Dori
Highwalk Street
1
3
Rico
NULL
NULL
i have query, but its not work
this is my query
SELECT
id_consumer,
name,
CASE WHEN (`consumer_address`.`status` = 1) THEN `consumer_address`.`address` ELSE NULL END as "Address",
CASE WHEN (`consumer_address`.`status` = 1) THEN `consumer_address`.`status` ELSE NULL END as "Status"
FROM consumer
JOIN consumer_address ON consumer_address.id_consumer = consumer.id_consumer
Thanks
Very simple solution:
SELECT
`id_consumer`,
`name`,
`consumer_address`.`address`,
`consumer_address`.`status`
FROM consumer
LEFT JOIN consumer_address ON
`consumer_address`.`id_consumer` = `consumer`.`id_consumer` AND
`consumer_address`.`status` = 1
Instead of using CASE WHEN just include the status in the JOIN.
Additionally, to keep consumer 3, you need a LEFT JOIN.
SELECT
id_consumer,
name,
`consumer_address`.`address`,
`consumer_address`.`status`
FROM
consumer
LEFT JOIN
consumer_address
ON consumer_address.id_consumer = consumer.id_consumer
AND consumer_address.status = 1

How can I get parent categories in a single query

I've a "Categories" table on my mysql database that has three columns: category_id, parent_category_id, category_name.
category_id is PK and Auto increment and parent_category_id holds a category's parent "categori_id". So I can relate categories in just one table.
For example:
category_id parent_category_id category_name
1 0 USA
2 1 California
3 2 San Francisco
4 0 Canada
5 4 Alberta
6 5 Calgary
I want to get a category and its all parents in a single query. This query should give me a result like:
USA > California > San Francisco
or
Canada > Alberta > Calgary
Opencart actually does something like this but it stores relationship data between categories in a different table. I want to do this in just one table and I've no idea what should I do.
This can be solved with only 1 simple query (SELECT * FROM categories), and a recursive function.
Bear with me. Run PHP code myself, checked, confirmed result.
PhpFiddle
//To begin set a category_id, say our node is San Fransisco, so $category_id=3
//get San Fransisco's id
$node = 3
/* Get your database into array structure like this
$categories = array(
'1'=>array('parent'=>'0', 'name'=>'USA'),
'2'=>array('parent'=>'1', 'name'=>'California')
..
)
*/
$categories = array(); //or [] as of PHP 5.6
$qr = mysql_query('SELECT * FROM categories');
while($row=mysql_fetch_assoc($qr))
{
$categories[$row['category_id']] = array('parent'=>$row['parent_category_id'], 'name'=>$row['category_name']);
}
//first element of tree, is San Fransisco, category_id=3
$tree=array($node);
function getCategoryTree($node, & $tree){
global $categories;
if($categories[$node]['parent']==='0')
return;
$parent_id = $categories[$node]['parent'];
$tree[]=$parent_id;
return getCategoryTree($parent_id, $tree);
}
//do magick
getCategoryTree($node, $tree);
//now you have $tree=array(3,2,1);
$out='';
while($last = array_pop($tree))
{
$out.= $categories[$last]['name'].' > ';
}
//outputs USA > California > San Francisco
echo $out;

customized JSON output in pig

Need customized JSON output--
(I have two files - text file and schema file)
abc.txt -
100002030,Tom,peter,eng,block 3, lane 5,california,10021
100003031,Tom,john,doc,block 2, lane 2,california,10021
100004032,Tom,jim,eng,block 1, lane 1,california,10021
100005033,Tom,trek,doc,block 2, lane 2,california,10021
100006034,Tom,peter,eng,block 6, lane 6,california,10021
abc_schema.txt (field name and position)
rollno 1
firstname 2
lastname 3
qualification 4
address1 5
address2 6
city 7
Zipcode 8
Rules-
First 6 characters of rollno
Need to club address1 | address2 | city
Prefix Address to above
Expected Output-
{"rollno":"100002","firstname":"Tom","lastname:"peter","qualification":"eng","Address":"block 3 lane 5 california","zipcode":"10021"}
{"rollno":"100002","firstname":"Tom","lastname:"john","qualification":"doc","Address":"block 2 lane 2 california","zipcode":"10021"}
{"rollno":"100004","firstname":"Tom","lastname:"jim","qualification":"eng","Address":"block 1 lane 1 california","zipcode":"10021"}
{"rollno":"100005","firstname":"Tom","lastname:"trek","qualification":"doc","Address":"block 2 lane 2 california","zipcode":"10021"}
{"rollno":"100006","firstname":"Tom","lastname:"peter","qualification":"eng","Address":"block 6 lane 6 california","zipcode":"10021"}
I do not wish to hardcode the fields but read from the schema file, the idea is to have reusable code. Something like looping schema file and the text file
A = load 'abc.txt' using PigStorage(',') as (rollno, Fname,Lname,qua,add1,add2,city,Zipcode);
B = foreach A generate rollno, Fname,Lname,qua,concate (add1,add2,city) ,Zipcode;
C= STORE B
INTO 'first_table.json'
USING JsonStorage();
Hope this helps.

get greatest Id from multiple table and remove duplicate value

i got some problem obtaining value from my db,this is my first db
ID_HARGA ID_USER ID_BAG_PEMASARAN ID_ITEM HARGA ENTRY_DATE
1 9 1 3 1000000 2015-01-11 09:55:27
2 9 1 5 2000000 2015-01-13 07:19:10
5 9 1 3 3000000 2015-01-13 13:47:32
6 9 1 43 7000000 2015-01-13 13:49:49
13 9 1 50 3000000 2015-01-13 17:56:54
37 9 1 50 100 2015-01-19 09:08:20
this is the second one
ID_ITEM NAMA_ITEM SPEC_ITEM MASA_GARANSI STATUS_ITEM DIR_IMAGE
3 water heater emas bagus sekali selamanya 1
5 water heater tembaga bagus super selamanya 1
43 water hater heater seupo 3 1 water_22.jpg
50 tankkk 50 Liter 1 1 water_2.jpg
i know there is some null value just igniore it, i already did this
public function get_item()
{
//menghitung jumlah varian barang yang ada
$querycounter = $this->db->query('select * from ITEM WHERE STATUS_ITEM = 1');
$counter = $querycounter->num_rows();
$this->db->select('ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,HARGA,DIR_IMAGE');
$this->db->from('ITEM');
$this->db->join('HARGA', 'ITEM.ID_ITEM = HARGA.ID_ITEM','left');
// $this->db->order_by('ENTRY_DATE','DESC');
//$this->db->limit($counter);
$this->db->where('STATUS_ITEM',1);
//$query = $this->db->query('select from ITEM i join HARGA h on i.ID_ITEM = h.ID_ITEM order by h.ENTRY_DATE ASC');
$query = $this->db->get();
return $query->result_array();
//$this->db->select('nama_item','spec_item');
//$query = $this->db->get('item');
}
what i want is,just ignore dir_image
ID_ITEM NAMA_ITEM HARGA DIR_IMAGE
3 water heater emas 1000000
5 water heater tembaga 3000000
43 water hater 7000000
50 tankk 100
ID_item with biggest number not the old , so everytime i am insert into id_harga with ID_ITEM
the result always the newest HARGA
Thx before
There is a little confession that, do you want to get the maximum hagra, or newest inserted data (identifiable by HAGRA_ID if its auto increment by 1) in Hagra table. Your Code should be like
$this->db->select('ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,max(HARGA),DIR_IMAGE');
$this->db->from('ITEM');
$this->db->join('HARGA', 'ITEM.ID_ITEM = HARGA.ID_ITEM','left');
$this->db->where('STATUS_ITEM',1);
$this->db->group_by('item.item_id');
This will join your tables and select maximum value of Hagra. To use Max function, you need to use group_by(CodeIgniter Documentation) clause, other wise it will show only the single maximum value. Group_by will help to show maximum value of each group (each item);
If you want to get the newest inserted data, you can use the same technique using
$this->db->select('MAX(HAGRA_ID),ITEM.ID_ITEM,NAMA_ITEM,SPEC_ITEM,HARGA,DIR_IMAGE');