I have some tables.
picture
people_ids type is TEXT. No foreign keys used.
main_id - is like main parent, one from PEOPLE table;
picture
I need SQL request to get result.
SELECT * from families ...
[
'main' => [
'name' => 'John',
'surname' => 'Brown'
],
'people' => [
[
'name' => 'John',
'surname' => 'Brown'
],
[
'name' => 'Merry',
'surname' => 'Brown'
],
[
'name' => 'Lizy',
'surname' => 'Brown'
]
]
]
Thanks for all, I'm dounded answer by the time)
Now I'm understanding that... that was maximally stupid idea. Learn more about SQL, table joining process and more.
Related
I just cant figure out how to get the day, the months and the year from the dates.
Here it is the eloquent
public function query(Detail $model)
{
return $model->newQuery()->leftjoin('fishers','fishers.id', '=','details.fisher_id')
->leftjoin('species','species.id', '=', 'details.species_id')
->leftjoin('purposes','purposes.id', '=', 'details.purpose_id')
->leftjoin('islands','islands.id', '=', 'fishers.island_id')
->leftjoin('preservations','preservations.id', '=', 'details.preservation_id')
->select('fishers.*','details.*','details.indate','islands.island_name','fishers.fisher_first_name','fishers.fisher_last_name','details.weight','species.species_name','purposes.purpose_name','preservations.preservation_name');
}
I have tried to use Month(details.indate) as Month, Year(details.indate) as Year but seems not working. I used date as the datatype for indate and when I do Month(details.indate) as Month I get this error:
SQLSTATE[42S22]: COLUMN NOT FOUND: 1054 UNKNOWN COLUMN 'Month(details.indate)' IN 'field list' (SQL: SELECT fishers.* , details.* , MONTH(details.indate) AS MONTH
This is my datatable columns
protected function getColumns()
{
return [
[ 'data' => 'purpose_name', 'name' => 'purposes.purpose_name', 'title' => 'Purpose' ],
[ 'data' => 'fisher_first_name', 'name' => 'fishers.fisher_first_name', 'title' => 'Fisher Name' ],
[ 'data' => 'preservation_name', 'name' => 'preservations.preservation_name', 'title' => 'Preservation Methods' ],
[ 'data' => 'species_name', 'name' => 'species.species_name', 'title' => 'Species Name' ],
[ 'data' => 'island_name', 'name' => 'islands.island_name', 'title' => 'Island Name' ],
[ 'data' => 'weight', 'name' => 'details.weight', 'title' => 'Weight' ],
[ 'data' => 'indate', 'name' => 'details.indate', 'title' => 'Month' ],
[ 'data' => 'indate', 'name' => 'details.indate', 'title' => 'Year' ],
];
}
can someone help me how to do it?
You need to use the DB::raw function to select expressions using Laravel Eloquent.
For example:
return $model->newQuery()->...
->select(DB::raw('Month(details.indate) as Month'), DB::raw('Year(details.indate) as Year'), 'fishers.*','details.*', ...)
Be sure to add use DB; at the top of your file.
I need return of my mysql query
from
SELECT name as id FROM table
but I need return like this in mysql (I wrote it in an array because it's easy to write)
[
20: 'Jack',//20 is id from table and Jack is name from table
40: 'Nano',
49: 'Hakase'
]
Use WHERE condition
SELECT * FROM table WHERE ID = yourId
just replace yourId for the ID number you want.
First, select all id and name using
SELECT id, name FROM table SOME CONDITIONS
this query would return a result like
$result = [
[ id => 1, name => 'name' ],
[ id => 1, name => 'name' ],
[ id => 1, name => 'name' ],
[ id => 1, name => 'name' ],
]
then arrange array
$accepted_output = array_column($result, 'id', 'name')
$accepted_output = [
[ 1 => 'name' ],
[ 1 => 'name' ],
[ 1 => 'name' ],
[ 1 => 'name' ],
]
array_column docs - https://www.php.net/manual/en/function.array-column.php
You want to return the ID and the name so you should rewrite your query to:
SELECT id,name FROM table
You should use commas to seperate field names.
For more information you can do some reading here.
I want to find all the records from column who have any keyword match with column data:
It works fine if searching single search keyword in column having comma separated values like below code: Cakephp 3.4 version
$posts = TableRegistry::get('Posts');
$search_keywords = array_filter(explode(' ', $search_string));
$option = [
'contain' => false,
'conditions' => [
"find_in_set('New', Posts.title)",
],
'order' => ['Posts.created DESC']
];
$allpost = $posts->find('all',$option)->toArray();
Note: i want all the words from string should be search with column title, its not mandatory to have comma separated records:
$search_keyword = "New car in new delhi";
so i want code to be like below :
$search_keyword = "New car in new delhi";
$search_keywords = array_filter(explode(' ', $search_string));
$option = [
'contain' => false,
'conditions' => [
"find_in_set({$search_keywords}, `Posts`.title)",
],
'order' => ['Posts.created DESC']
];
$allpost = $posts->find('all',$option)->toArray();
Great Thanks in advance!!!
I found the solution:
First make column fulltext index then add below code:
$search_string = "New car in new delhi";
$option = [
'contain' => false,
'conditions' => [
"MATCH (title) AGAINST ('$search_string')"
],
'order' => ['Posts.created DESC']
];
$allpost = $posts->find('all',$option)->toArray();
I remember to have done this before, but now it does not work and I can't get it out.
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
]
],
The output is
1
2
2
1
instead of
Male
Female
Female
Male
What is the problem now? I'd swear I used it just the same way but ...
I do it like this
[
'label' => 'Sex',
'attribute' => 'gan_sex',
'filter' => [
'1' => 'Male',
'2' => 'Female'
],
// translate lookup value
'value' => function ($model) {
$gender = [
'1' => 'Male',
'2' => 'Female'
];
return $gender[$model->gan_sex];
}
]
Possible values for gan_sex must be restricted to 1 and 2.
My array is :
$response = [
0 => [
'id' => 'US',
'text' => 'United States'
],
1 => [
'id' => 'CA',
'text' => 'Canada'
],
2 => [
'id' => 'FR',
'text' => 'France'
],
...
]
When I do a json_encode on it, for some reason the id value is 0 through the array:
{"id":0,"text":"United States"},
{"id":0,"text":"Canada"},
{"id":0,"text":"France"}
This only happens if the the column name is id as if json_encode forces ID to be numeric.
Any idea how to use a string in the id column ?
You need commas after all of your 'id' => 'XX' lines. After adding those and running json_encode it worked fine for me, the id's all kept their string values.
Try:
$response = [
0 => [
'id' => 'US',
'text' => 'United States'
],
1 => [
'id' => 'CA',
'text' => 'Canada',
],
2 => [
'id' => 'FR',
'text' => 'France'
]
];
var_dump(json_encode($response, 20));
RESULT:
{
"0": {"id": "US", "text": "United States"},
"1": {"id": "CA", "text": "Canada"},
"2": {"id": "FR", "text": "France"}
}