How to find id which is string with semicolon - mysql

I want to find assigned jury id from the table.Problem is all that id are joint with semicolon
Here are the records.
id jury_id
1 2;4;6
I want to find jury_id=4. How can i find that record ?
Any help will be appreciated.

Use find_in_set():
where find_in_set(?, replace(jury_id, ';', ',')) > 0
Replace the ? with your target value.
Note that to use this function the search string must be a CSV, so you must replace the semicolons with commas.

there are two ways
1.
let say jury_id came as $ids;
$array=explode(',',$ids);
if(in_array('4',$array)
{
echo "id is in there";
}
2.
let say jury_id came as $ids;
if(strpos($ids,'4')==true)
{
echo "id is in there";
}

Like what F. Muller said, you can try it like
SELECT id,jury_id from table WHERE jury_id LIKE %$the_jury_id%

First remark is that it is an absolutely terrible database design.
To answer the question, you should be able to solve your problem with the LIKE operator:
id LIKE '%;4;%' or id LIKE '4;%' or id LIKE '%;4'

Try with
select * from tbl_name where jury_id like '%jury_id%'

Related

Replace template smart tags <<tag>> to [tag] in mysql

I have an table name templateType, It has column name Template_Text.
The Template have many smart tags <> to [tag] using mysql, and I need to replace << to [ and >> with ].
Edit from OP's comments:
It is an template with large text and having multiple smart tags. As example : " I <<Fname>> <<Lname>>, <<UserId>> <<Designation>> of xyz organization, Proud to announce...."
Here I need to replace these << with [ and >> with ], so it will look like
" [Fname] [Lname], [UserId] ...."
Based on your comments, your MySQL version does not support Regex_Replace() function. So, a generic solution is not feasible.
Assuming that your string does not contain additional << and >>, other than following the <<%>> format, we can use Replace() function.
I have also added a WHERE condition, so that we pick only those rows which match our given substring criteria.
Update templateType
SET Template_Text = REPLACE(REPLACE(Template_Text, '<<', '['), '>>', ']')
WHERE Template_Text LIKE '%<<%>>%'
In case the problem is further complex, you may get some ideas from this answer: https://stackoverflow.com/a/53286571/2469308
A couple of replace calls should work:
SELECT REPLACE(REPLACE(template_text, '<<', '['), '>>', '])
FROM template_type

Rails where clause match integer with the array

I have an issue in finding the correct records from the database. This is my query:
#interested_individual = Profile.where('category_id = ?', #event.interests).all
Where category_id is the column in the profiles table what holds the integer value and #event.interests returns an array that is ["1", "2", "3"]
What I want here is to fetch all the profiles where its category_id is present in the #event.interests array.
Please let me know the solution. Thanks in advance
You don't really need to manually write the binding with ? and all.
Try this:
#interested_individual = Profile.where(category_id: #event.interests).all
Active Record will then turn that into category_id IN (?) for you if you pass in an array.
This query will translate to category_id IN [interest_ids...]
Profile.where(category_id: #event.interests).all
You were close. Use IN.
#interested_individual = Profile.where('category_id IN (?)', #event.interests).all
But, I would suggest find you here, if you are only wanted to get collection of all Profile records :
#interested_individual = Profile.find(#event.interests)

SQL - How do I replace/modify strings in multiple rows?

I have a lot of rows in database where column "info" is ie. "Size, inch: 12x13<br>Material: paper<br>Amount: 100pcs". Now I need to find/select all rows that has this string part "Size, inch:" and replace/fix those rows' colums to ie. "Size: 12x13 inch<br> Material: paper<br>Amount: 100pcs ".
How in the world I write a sql statement for this? Do I need some magic regexp for sql? How do I replace and modify parts of a string in multiple rows?
EDIT: The numbers can be anything (ie. 12x13 or 44x55 or 77x88x99 etc.) So there would have to be some kind of wildcard for the numbers, perhaps?
I need to change "Size, inch: 'anynumbershere' 'anythingafter the numbers'" to "Size: 'anynumbershere' inch 'anythingafter'".
update mytable set info=REPLACE ( info , 'Size, inch: 12x13 ', 'Size: 12x13 inch' )
where info like '%inch:12X13%'
Perhaps this will work:
update table t
info = replace(replace(info, 'Size inch:', 'Size:'), '<br>Material', ' inch<br>Material')
where info like 'Size inch:%<br>Material%';
Note: this assumes that 'Size inch' and '<br>Material' only appear once in each string.

phpmyadmin - category as dbname?

I don't know how to specify the title for the question, and that's why i cannot find the answer by my own.
I'll try to explain what I mean.
We have a database. Then, on the list of the databases there's something like category? it's unclickable and it's bold. It contains three databases preceded by underscore and each of this databases contains tables.
It looks something like:
Category
> _something1
table1, table2, table3...
> _something2
table1, table2, table3...
> _something3
table1, table2, table3...
How it's called and how can I reach the effect above?
Here, "Category" is a prefix for database names. So your databases names are
category_something1
category_something2
category_something3
They are displayed in a collapsible/expandable tree.
From what i understood from the question, You want to write a program that will show 6 rows like this:
Category: -Something 1 table1,table2,table3
-Something 2 table1,table2,table3 -Something 3 table1,table2,table3
First of all you would need to form the rows in ASC order.
Change some thing like this :
Array(Category,
Category_something1,
Category_something2_table1,
Category_something2_table2,
Category_something2_table3,
Category_something2,
Category_something3_table1,
Category_something3_table2,
Category_something3_table3,
Category_something3,
Category_something1_table1,
Category_something1_table2,
Category_something1_table3,)
into :
Array(Category,
Category_something1,
Category_something1_table1,
Category_something1_table2,
Category_something1_table3,
Category_something2,
Category_something2_table1,
Category_something2_table2,
Category_something2_table3,
Category_something3,
Category_something3_table1,
Category_something3_table2,
Category_something3_table3,)
Then Run a loop that splits each string into 3. if second value/third is not present print as heading/category else print as item/something.
While(ArrayKey is not equal to count(Array))
{
SplitString(ArrayCurValue,"_",$VarValue1,$VarValue2,$VarValue3); //split current string into 3 vars where _ is present
if($VarValue2 == "")
{
Print "<b>",$VarValue1,"</b>";
}else{
if($VarValue3 == "")
{
Print " -",$VarValue2;
}else{
Print " -",$VarValue3;
}
}
}

DBIx::Class Temporary column

I am using DBIx::Class and I have a query like this:
$groups = $c->model('DB::Project')->search(
{ "sessions.user_id"=>$c->user->id,done_yn=>'y' },
{
select => ["name", "id",\'SUM(UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(start_time)) as total_time'], #\''
join => 'sessions',
}
);
I'd like to be able to get the value of SUM(UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(start_time)), but because this is not a real column in the table, referencing total_time for a DBIx::Class::Row object doesn't seem to work. Does anyone know how I can get these temporary columns? Thanks!
The select docs describe perfectly how to achieve what you're trying to accomplish.
It's also recommended to avoid literal SQL when possible, you can use { sum => \'UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(start_time)' } instead.
The 'as' in the literal SQL isn't required to give the column a name, you have to use either the as search attribute or better the columns shortcut instead of select+as.