how to select column as `another name` in Kohana - kohana

I'm using Kohana. I'm trying to select a column as some name but it's giving error -
Table 'users AS U' doesn't exist
How to make this alias?
$result = DB::select( 'users.firstname',
'users.lastname',
'U.firstname AS alt_firstname',
'U.lastname AS alt_lastname'
)
->from('mytable')
->join('table2', 'INNER')
->on('mytable.id', '=', DB::expr('table2.id'))
->join('users', 'INNER')
->on('users.id', '=', DB::expr('mytable.id'))
->join('users AS U', 'INNER')
->on('U.id', '=', DB::expr('table2.id'))
->execute();
return $result;

$query = DB::select(array('username', 'u'), array('password', 'p'))->from('users');
https://kohanaframework.org/3.3/guide/database/query/builder#select-as-column-aliases

Related

Excel Pivot Table_Same column and row item with more than one values

I have a table like this
I hope I can get the latest value when a product has more than one value in a same measurement item by excel pivot table. Like this:
I tried so hard but still have no way to achieve this...
Any idea? Thanks.
You'll need to add your source data to the Data Model (via the Power Pivot ribbon), after which you can create the following measures:
Weight
VAR LatestDate =
MAXX( 'Table', IF( 'Table'[measure item] = "weight", 'Table'[time] ) )
RETURN
MAXX(
'Table',
IF(
'Table'[measure item] = "weight",
IF( 'Table'[time] = LatestDate, 'Table'[value] )
)
)
Height
VAR LatestDate =
MAXX( 'Table', IF( 'Table'[measure item] = "height", 'Table'[time] ) )
RETURN
MAXX(
'Table',
IF(
'Table'[measure item] = "height",
IF( 'Table'[time] = LatestDate, 'Table'[value] )
)
)
Create a Pivot Table with these two measures in the Values field together with product name in the Rows field.

Get Compiled Select gives only initial table CodeIgniter 4

Hello guys getCompiled Select in CodeIgniter returns only
Select * from tablename don't have any joins and wheres
if (!empty($requestArr)) {
$this->data['listings'] = $listingModel
->join('countries', 'listings.country_id=countries.country_id', 'LEFT')
->join('states', 'listings.state_id=states.state_id', 'LEFT')
->join('cities', 'listings.city_id=cities.city_id', 'LEFT')
->join('purposes', 'listings.purpose_id=purposes.purpose_id', 'LEFT')
->join('sub_purposes', 'listings.sub_purpose_id=sub_purposes.sub_purpose_id', 'LEFT')
->join('types', 'listings.type_id=types.type_id', 'LEFT')
->join('sub_types', 'listings.sub_type_id=sub_types.sub_type_id', 'LEFT')
->orLike('area',"%".$requestArr['area']."%")
->orWhere('listings.city_id', $requestArr['city_id'])
->orWhere('listings.state_id', $requestArr['city_id'])
->where('land_area', $requestArr['pSize'])
->where('land_unit', $requestArr['area_unit'])
->where('bedrooms', $requestArr['bedrooms'])
->where('bedrooms', $requestArr['bedrooms'])
->where('listings.purpose_id', $requestArr['purpose_id'])
->where('listings.type_id', $requestArr['type_id'])
->orderBy('listings.created_at', 'DESC')
->paginate(6);
echo $listingModel->getCompiledSelect();
echo json_encode($this->data['listings']);
I want to print the whole generated query but only give result in the below screenshot

Assign Column Name for Simple Coalesce Statement

While attempting to create a list of all ID's made since _____ I am able to get the results I want from the following:
DECLARE #BoID varchar(max)
SELECT #BoID = COALESCE(#BoID + ', ', '') +
CAST(ApplicationID AS varchar(10))
FROM BoList as "ID"
WHERE CreatedDate > '2017-07-01 18:14:09.210'
However, I am having issues with establishing a column name for the above statement. Where does the as "ID" need to be located at in order to give the above result a column name of "ID"?
As the query stands now, you are giving the table BoList an alias of "ID" instead of the column. Since you are selecting the value into a variable there is no output. You can do it like this...
SELECT COALESCE(#BoID + ', ', '') +
CAST(ApplicationID AS varchar(10)) as "ID"
FROM BoList
WHERE CreatedDate > '2017-07-01 18:14:09.210'
Or if you really do need to stash the value in a variable to return later as part of another query...
DECLARE #BoID varchar(max)
SELECT #BoID = COALESCE(#BoID + ', ', '') +
CAST(ApplicationID AS varchar(10))
FROM BoList
WHERE CreatedDate > '2017-07-01 18:14:09.210'
SELECT #BoID AS "ID", other columns... FROM whatever

Sub-query in eloquent with concat

I'm new laravel 5.3 and facing sub queries problem with eloquent. i don't know how to write sub-query using eloquent. My query is given below.
select concat(m, '-', y), total
FROM (
select month(`date`) as m , year(`date`) as y, round(sum(amount)) as total
from `budget`
where
`user_id` = 1 and
`amount` is not null
group by m, y
) as t
This is working fine in MySQL . so how can we convert this query in eloquent. so kindly solve this problem
You would need to create the sub query first you can merge the bindings in the parent query:
$sub = Budget::selectSub('month(`date`)', 'm')
->selectSub('year(`date`)', 'y')
->selectSub('round(sum(amount))', 'total')
->where('user_id', 1)
->whereNotNull('amount')
->groupBy('m', 'y');
$data = DB::table(DB::raw("({$sub->toSql()}) as t"))
->mergeBindings($sub->getQuery())
->selectRaw("concat(m, '-', y)")
->addSelect('total')
->get();

Database error Duplicate entry

So i have this error when i have duplicate values while inserting data, now my query is correct, i've tried to run it in phpmyadmin, and works just fine, but kohana gives me errors, this is how my query looks like:
DB::query(Database::INSERT, 'INSERT INTO `views` ( user_id, viewer_id, username, picture, view_time, view_count)
VALUES ("'.$this->request->param('id2').'", "'.$user->id.'", "'.$user->username.'", "'.$user->picture.'", '.DB::expr('NOW()').', "1")
ON DUPLICATE KEY UPDATE `view_time` = NOW(), `view_count` = view_count +1
And in pure sql:
INSERT INTO `views` ( user_id, viewer_id, username, picture, view_time, view_count )
VALUES (
"134173", "139173", "username", "pic.jpg", NOW( ) , "1"
) ON DUPLICATE
KEY UPDATE `view_time` = NOW( ) ,
`view_count` = view_count +1
So basicly i try to insert and on duplicate values i update, but for some reason kohana gives me error:
kohana Database_Exception [ 1062 ]: Duplicate entry
How can i remove this error?
Perhaps try this approach by not specifying the query type:
$query = "
INSERT INTO `views` (user_id, viewer_id, username, picture, view_time, view_count)
VALUES (%d, %d, '%s', '%s', %s, %d)
ON DUPLICATE KEY UPDATE `view_time` = %s, `view_count` = view_count + 1";
$query = sprintf($query, $this->request->param('id2'), $user->id, $user->username, $user->picture, DB::expr('NOW()'), 1, DB::expr('NOW()'));
$result = Database::instance()->query(NULL, $query);

Resources