how to tokenize string in record to be row - mysql

I have the data in a database table named term like this :
---------------------------------------
id_term | keyword |
------- | -----------------------------
1 | how to make search engine |
2 | application engineering |
3 | android application example |
--------------------------------------
then I want it to be like this table :
----------------------------------
| id_term | keyword |
----------------------------------
1 | how |
1 | to |
1 | Make |
1 | search |
1 | engine |
2 | application |
2 | engineering |
3 | example |
3 | application |
3 | android |
----------------------------------
I've tried googling to find references to split the string, but still have not found the appropriate expectations. In an experiment that I've done using substring_index results I could actually like this:
---------------------------------------
id_term | keyword |
------- | -------------------------------
1 | how to make search engine |
1 | how to make search engine |
1 | how to make search engine |
--------------------------------------
there anything you can help me or has the solution of my problem? mysql code that I try something like this:
select term.id_kata, SUBSTRING_INDEX (SUBSTRING_INDEX (term.keyword, ',', term.id_kata), ',', -1) keyword term from inner join keyword_doc on CHAR_LENGTH (term.keyword) -CHAR_LENGTH (REPLACE (term.keyword , ',', ''))> = term.id_kata-1 ORDER BY `term`.`keyword` DESC
I've tried googling for approximately 5 hours to find a solution, but have not found until I was confused to be asked where. there any ideas or can help provide a solution?

The solution for it problem is please take 'BETWEEN' in SQL SYNTAX. this code 100% work for it problem :
<?php
#connection
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pasword';
$dbname = 'yourdatabasename';
error_reporting(E_ALL ^ E_DEPRECATED);
mysql_connect($dbhost,$dbuser,$dbpass) or die(mysqli_error('cannot connect to the server'));
mysql_select_db($dbname) or die(mysqli('database selection problem'));
$frequency = array();
$datastring = 'SELECT id_term,keyword FROM data_token WHERE id_term BETWEEN 1 AND 3';
mysql_select_db('yourdatabasename');
$calldata = mysql_query($datastring);
while($takedata = mysql_fetch_array($calldata,MYSQL_ASSOC))
{
$array = explode("\n",$takedata['keyword']);
foreach ($array as $index => $keyword)
{
if(! array_key_exists($keyword,$frequency))
{
$frequency[$keyword] = 1;
}
else
{
$frequency[$keyword] = $frequency[$keyword] + 1;
}
}
$document = $takedata['id_term'];
foreach ($frequency as $term => $tf)
{
$sqlInput = "INSERT INTO yourtablename (id_term,keyword,frequency) VALUES ('$dokumen','{$term}', {$tf})";
mysql_query($sqlInput);
}
}
?>

Related

Mysql / Laravel: Search keywords by multi languages but only get the results of current language

"language" table
id | name | code
-------------------
1 | English | en
2 | German | de
"article" table
id | is_active
--------------
1 | 1
2 | 1
"article_translation" table
article_id | language_id | title
-------------------------------------------
1 | 1 | foo
1 | 2 | title1 in German
2 | 1 | title2 in English
2 | 2 | foo
Suppose I search "foo" with like condition, and I want to get the results with language_id = 1 but still need to search the title on other languages. The following results as I expected:
article_id | language_id | title
-------------------------------------------
1 | 1 | foo
2 | 1 | title2 in English
In laravel, I tried to use group by function but the results can't make sure that the language_id is 1. Also, it need to set the strict mode to false.
\App\ArticleTranslation::where('title', 'like', '%foo%')->groupBy('article_id')->paginate(10);
SELECT at1.*
FROM article_translation at1
JOIN article_translation at2 ON at1.article_id = at2.article_id
WHERE at2.title = #title
AND at1.language_id = #language_id
fiddle
PS. Convert to Laravel by yourself...
Add relations for easy queries
// Language model
public function articles()
{
return $this->belongsToMany(Article::class);
}
// Article model
public function languages()
{
return $this->belongsToMany(Language::class);
}
// in controller
$articles = Language::find(1)->articles()->wherePivot('title', 'like', '%foo%')->get();
foreach($articles as $article){
dump($article); // $title = $article->pivot->title
}

How to add incremental values to mysql database after sorting entries

ID | LastName | FirstName | Ordered
1 | Smith | John |
2 | Smith | Larry |
3 | Jones | Fred |
4 | Johnson | Todd |
Desired result: Update the Ordered field with incremental values in alphabetical order.
1 | Smith | John | 3
2 | Smith | Larry | 4
3 | Jones | Fred | 2
4 | Johnson | Todd | 1
$result = mysql_query("SELECT * FROM MyDatabase ORDER by
LastName,FirstName");
$N=0;
while ($row = mysql_fetch_array( $result ))
{
mysql_query("
UPDATE MyDatabase
SET Ordered = $N + 1
WHERE ...");
}
I know I need the WHERE but I can't seem to make any WHERE clauses work. I always end up with all the same numbers in the Ordered field. What would make this work as intended?
Sounds like you first need to get a total count of all rows in the result set (since you're storing from high to low you need to know the high). Then just decrement that value during the UPDATE / loop
Well, I figured this out to make it work. Just need to fetch the row and then compare it to the ID field as follows:
$result = mysql_query("SELECT * FROM MyDatabase ORDER by LastName, FirstName");
$N=1;
while ($row = mysql_fetch_array( $result ))
{
$updateid = $row['ID'];
mysql_query("
UPDATE MyDatabase
SET Ordered = $N
WHERE ID='$updateid'");
$N++;
}

Need a SQL query to get a value by joining two table in codeigniter

I have two tables.
Table 1: table_company
+---------------------------+
| company_id | company_name |
+---------------------------+
| 1 | Apple |
| 2 | Samsung |
+---------------------------+
Table 2: table_products
+------------+--------------+-------------+-----------+
| product_id | product_name | category_id |company_id |
+-----------------------------------------------------+
| 1 | iPhone | 3 | 1 |
| 2 | galaxy | 3 | 2 |
| 1 | iPad | 4 | 1 |
| 2 | tab | 4 | 2 |
+-----------------------------------------------------+
I want to join this 2 tables to get the company name according to category_id.
I wrote the following code in my model. but did not get anything. Please help.
public function select_company_by_category_id($category_id) {
$this->db->select('*');
$this->db->from('tbl_products');
$this->db->join('tbl_company', 'company_id = company_id');
$this->db->where('category_id', $category_id);
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
try to replace your join with this:
$this->db->join('tbl_company', 'tbl_company.company_id = tbl_products.company_id');
you can find more examples in codeigniter active record page
Use Left Join for this
public function select_company_by_category_id($category_id) {
$this->db->select('*');
$this->db->from('table_products');
$this->db->join('table_company', 'table_company.company_id = table_products.company_id', 'left'); # Changed
$this->db->where('table_products.category_id', $category_id); # Changed
$query = $this->db->get(); # Improved
$result = $query->result_array(); # Improved
return $result;
}
First of all open your database error from database.php file only on development line not on production.
Than issue is that company_id is available in both tables with same name than you must need to add table alias as:
public function select_company_by_category_id($category_id)
{
$this->db->select();
$this->db->from('table_products');
$this->db->join('table_company', 'table_company.company_id = table_products.company_id');
$this->db->where('table_products.category_id', $category_id);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

sort a field in my table in MySQL

I have a set of data like this:
ID | Name | Code
1 | John | ygkj
2 | Mike | ghyy
3 | Jay | uuja
And I want to use a function/stored procedure to change the "Code" column into the following:
ID | Name | Code
1 | John | gjky
2 | Mike | ghyy
3 | Jay | ajuu
Or maybe throwing it as a result of a SELECT statement. How can I do it with MySQL?
Thanks!!
Finally, I've done it via PHP. If anyone needs it, I'll post my script here
$inorder = array();
$result = $mysqli->query("select ID, Code from top");
while ($line = $result->fetch_assoc()) {
$stringParts = $line['Code'])
sort(str_split($stringParts);
$inorder[$line['ID']] = implode('',$stringParts);
}
foreach($inorder as $k=>$l)
{
$mysqli->query("update codes SET Code = '".$l."' where id_top = ".$k);
}
It seems like MySQL doesn't have complex array operations to work with the data within the results.

MySQL Update query with Dynamic contenct

In MySQL, I have two tables:
CATEGORY
----------------------------------------------
| category_name | category_code |
----------------------------------------------
| Suit | SU |
| Western | WE |
----------------------------------------------
PRODUCT
---------------------------------------------
| name | Category | code |
---------------------------------------------
| xyz1 | Suit | |
| abc1 | Suit | |
| abc2 | Western | |
---------------------------------------------
I want to update code in PRODUCT, so that after updation it is will:
PRODUCT
---------------------------------------------
| name | Category | code |
---------------------------------------------
| xyz1 | Suit | SU/0001 |
| abc1 | Suit | SU/0002 |
| abc2 | Western | WE/0001 |
---------------------------------------------
thanks in advance
Honestly, it looks like you need restructure your database. Each table should have an auto-incrementing "id" field. This would allow the "Category" field to reference an id instead of a duplicate of the name (in case you EVER decide to change the spelling or name of that category). Even down to your naming - if your field is IN the category table, it doesn't seem necessary to have "category_" before the "code" field name...etc
Beyond that, you're probably talking about a PHP script (or something similar) - this would allow you to repeat through each item and keep track of how many Suit codes you've added so you can increment your "code" field (I assume that's what the 0001 0002 is doing.
Off the top of my head, it'd be something like this (I'm sure it could be more streamlined/better - but hopefully this gives an idea of a way you could do it:
$numSU = 0;
$numWE = 0;
$products = mysql_fetch_array(mysql_query("SELECT * FROM product"));
foreach($products as $p) {
$codeQuery = mysql_query("SELECT category_code FROM category WHERE category_name='".$p['Category']."'");
$code = mysql_result($codeQuery,0,'category_code');
if($code == "SU") {
$numSU++;
$numItems = $numSU;
} else if($code == "WE") {
$numWE++;
$numItems = $numWE;
}
$update = mysql_query("UPDATE product SET code='".$code."/".$numItems."'");
}