I want to get array in database,it work in database query (my database i use postgrest) but not work in my project laravel.
Error message -> Undefined column: 7 ERROR: column sample1.text[1] does not exist
table sample1
___________________________________
| id | name | text[] |
|------+------+--------------------|
| 1 | aa | {xxx1,xxx2,xxx3} |
| 2 | bb | {xxx1,xxx2,xxx3} |
| 3 | cc | {xxx1,xxx2,xxx3} |
|______|______|____________________|
I need value
{
xxx1,
xxx1,
xxx1
}
my code
$search = DB::table("sample1")->select(array( 'sample1.text[1]'))->get();
return response()->json($search);
in my database query it work
SELECT "NSO_STAT_GIS"."BND_SET_STAT_TABLE"."MAPTB_CAT_SL_ID"[1] FROM "NSO_STAT_GIS"."BND_SET_STAT_TABLE"
$textsJson = DB::table("sample1")->select('text')->get();
$textArray = json_decode($textsJson);
https://laravel.com/docs/5.8/queries#selects
Related
I need help to make query to group by content of field. I have the following Database:
+----+--------------+-----------------+------------------------------+
| id | depart_kode | depart_task | value |
+----+--------------+-----------------+------------------------------+
| 1 | 001 | Create Profil | Some Value |
| 2 | 001.12 | Create Profil | Some Value |
| 3 | 001.452 | Create Profil | Some Value |
| 4 | 002.914 | Create Profil | Some Value |
| 5 | 002 | Create Profil | Some Value |
| 6 | 003 | Create Profil | Some Value |
+----+--------------+-----------------+------------------------------+
I need to make a query where I group by depart_kode and I should print by the first 3 digits like this in format json
"001":[
{
"depart_kode":"001,
and other
},
{
"depart_kode":"001.12"
},
],
"002":[
{
"depart_kode":"002"
}
]
something like that, most importantly they will be grouped in an array based on the first 3 numbers like 001.
and json just an example, which I want to split into an array based on the first 3 digits
Something like this?
$departs = [];
foreach ($sqlRows as $row) {
$departKey = substr($row['depart_kode'], 0, 3);
if (!array_key_exists($departKey, $departs)) {
$departs[$departKey] = [];
}
$departs[$departKey][] = $row['depart_kode'];
}
Working example.
references
substr - get a specific part of a string (first 3 chars in our example)
array_key_exists - checks if the key in an array exists
Suppose I have a table named testdb which has the following values:
| Name | Phone |ID |
|------------------|------------|---|
| supriyo roy | 9038689236 | 2 |
| supriyo banerjee | 8013147879 | 3 |
| Alex ghosh | 8985855665 | 4 |
Now, I am performing a search by the name and the input given is just "supriyo" (no last name given).
The search result should show both the values:
| Name | Phone |ID |
|------------------|------------|---|
| supriyo roy | 9038689236 | 2 |
| supriyo banerjee | 8013147879 | 3 |
Is this possible to achieve using MySQL and php?
You are looking for LIKE:
select *
from your_table
where name like 'supriyo%';
Demo
Edit:
In php, use:
select *
from your_table
where name like '$name%';
i have this table
id | part_number | desc | date |
1 | 3yrt3470 | aa | 2017-02-20 |
2 | 3utitj40 | bb | 2017-02-12 |
pk=id
i want to create query insert into table but if part_number exist it will updated. anyone can help me?
i tried this
function replace($part_number)
{
$this->db->replace('tbl_master_data');
$this->db->set('description',$description);
$this->db->set('rrp',$rrp);
$this->db->set('discount_code',$discount_code);
$this->db->set('product_group',$product_group);
$this->db->set('value_discount',$value_discount);
$this->db->set('stk',$stk);
$this->db->where('part_number',$part_number);
}
error messages show you must use the 'set' method...
I have a table like this in my database.
| id | yes_answer | no_answer |
| a01 | a05 | a04 |
| a03 | a16 | a32 |
| a04 | c01 | a05 |
| a05 | a06 | c06 |
| a06 | a08 | a09 |
| a08 | c03 | a09 |
| a09 | a03 | |
| a16 | a33 | a02 |
| a32 | c04 | a16 |
| a33 | c05 | |
As An Explanation :
1.id column represent an id of question. it is linked to another table on my database.
2.yes_answer column represent the next question (like a01) or conclusion (like c04) you get when you answer the question represented by id column with yes or TRUE or 1 value.
3.no_answer is generally the same as yes_answer column except that you get it when you answer question represented by id column with no, FALSE, 0 value.
4. Every id, yes_answer, no_answer is connected each other. The Result is A Pattern. When you Want to find a path that lead to answer c01 it will be like c05 -> a33,a16,a32,a03,a09,a06,a05,a01.
And Here I am Trying To Bactrack The Pattern From answer column that marked with c like c01 or c02 like above. And I've been able to bactrack only the beginning value of it Like (c05 -> a33) and the rest is blank.
Here is My Code
$sqlQueryYes="SELECT * FROM tbl_path_inference WHERE yes_answer='c05'";
$execQueryYes=mysql_query($sqlQueryYes);<br />
$queryStatus=mysql_num_rows($execQueyYes); //Getting Query Status<br />
if($queryStatus == 1) {
$getDataYes=mysql_fetch_array($execQueryYes);
$ansPath=$ansPath." ".$getDataYes['id'];
} else { // if cant find answer on column yes_answer try on no_answer
$sqlQueryNo="SELECT * FROM tbl_path_inference WHERE no_answer='c05'";
$execQueryNo=mysql_query($sqlQueryNo);
$getDataNo=mysql_fetch_array($execQueryNo);
$ansPath=$ansPath." ".$getDataNo;
}
Anyone Have Solution On It. Im Still working It Now.
I have a function that runs and gets the labels from a lookup table for values stored in a particular table. When there is 1 value it displays correctly. However when there is multiple values it only returns the first one. For example:
Lookup table is
| Tel_No| Tel_type |
--------------------
| 1 | Info |
| 2 | Support |
| 3 | Call |
Main table is
| TelephoneCalls |
------------------
| 1,3 |
| 3 |
| 1,2,3 |
The function I have at the moment which works for matching 1 value is
function getMonitoring($monitoring){
$query = "SELECT Tel_type FROM TelephoneCalls Where Tel_no = '$monitoring'";
$result9 =mysql_query($query) or die(mysql_error());
list($Tel_type) = mysql_fetch_array($result9, MYSQL_NUM);
return $Tel_type;
}
How can I get it to list the values like below
If 1, 3 then display Info, Call
If 3 display Call
If 1, 2, 3 display Info, Support, Call
Thanks for any help!
I guess the comments touched upon it, but you really should change your schema to be more of a many-to-many relationship than using CSV values in the fields. If you can't this query should work:
SELECT telephonecalls, GROUP_CONCAT(tel_type)
FROM lookup_table
LEFT JOIN main_table ON FIND_IN_SET(tel_no , replace(TelephoneCalls,' ', '')) > 0
GROUP BY telephonecalls;
#outputs
+----------------+------------------------+
| telephonecalls | GROUP_CONCAT(tel_type) |
+----------------+------------------------+
| 1,2,3 | Info,Support,Call |
| 1,3 | Info,Call |
| 3 | Call |
+----------------+------------------------+
http://sqlfiddle.com/#!2/194fd/1/0