Unknown column 't.faic' in 'field list' - php

When I execute my query i got an error
[Err] 1054 - Unknown column 't.faid' in 'field list'
I have a table called
app_interview with fields 'atid','atic','atname','inttotal'
applicants with the same atic are just one person.
I want to total all the total of applicant with same atic field value and divide it with 7 and save the average score to a new table called:
afnup_worksheet with field 'faid','faic','fnl_name','ftotal'
heres my current query:
INSERT INTO afnup_worksheet (faid,faic,fnl_name,ftotal)
SELECT DISTINCT
t.faid
,t.faic
,t.fnl_name
,(SELECT SUM(t2.inttotal) FROM app_interview t2 WHERE t2.atic = t.faic)/7 thissum
FROM app_interview t
GROUP BY atname HAVING COUNT(DISTINCT atic)=1

You.re selecting t.faic, where t refers to app_interview, but according to the details you have posted, app_interview doesn't conatin a field called faic. This is also true for t.faid and t.fnl_name.

According to your description, app_interview does not have a faid column

Related

How to aggregate query on related table's field in Laravel?

I have a One To Many (Inverse) relation on my laravel 5.4 application. There are two models Sale and Vehicle which are related and associated with the scene.
The relation on the Sale model is :
public function vehicle()
{
return $this->belongsTo('App\Models\Vehicle','vehicle_id');
}
Table sales has following fields :
id, vehicle_id, date_time, status etc.
Table vehicles has following fields :
id, reg_number, volume, status etc.
What I want is, sum of the field 'vehicles.volume' of Sale records within the search conditions.
I have tried some methods like the following one:
$query = Sale::where('status', 1);
$query = $query->where('date_time', '<', "2017-05-10 10:10:05");
$totalVolume = $query->whereHas('vehicle')->sum('vehicles.volume');
and it resulted in the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'vehicles.volume' in 'field list' (SQL: select
sum(vehicles.volume) as aggregate from sales where status = 1
and date_time < "2017-05-10 10:10:05" and exists (select * from vehicles where sales.vehicle_id =
vehicles.id))
hopefully waiting for a solution to 'get the sum of the volume of the sales' using eloquent query.
Edited
You need to use a join before summing the vehicles.volume column
$totalVolume = Sale::where('status', 1)
->where('date_time', '<', "2017-05-10 10:10:05")
->join('vehicles', 'vehicles.id', '=', 'sales.vehicle_id')
->select(DB::raw('sum(vehicles.volume) as total_volume');
select sum(volume) as aggregate from vehicles INNER JOIN sales ON vehicles.id=sales.vehicle_id

Join in Pivot tabel Laravel

I have following tables:
Food:
Foodid(pk)
FoodName
FoodImage
Description
Categories:
Category_id(pk)
CategoryName
Restaurant
Res_id(pk)
ResName
Address
category_food_restaurant
Category_id(fk)
Food_id(fk)
Res_id(fk)
Now I want to show Food items based on category name.For that I make query as:
$Category = DB::table('Food')
->select('Food.Food_id','Food.FoodName',
'Food.FoodImage','Categories.CategoryName')
->join('Categories','Categories.Category_id',
'=','category_food_restauarant.Category_id')
->where('Categories.CategoryName',
'=','Breakfast')->get();
But this gives me error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Food.Category_id' in 'on clause' (SQL: select Food.Food_id, Food.FoodName, Food.FoodImage, Categories.CategoryName from Food inner join Categories on Categories.Category_id = Food.Category_id where Categories.CategoryName = Breakfast).
Where is the problem.How I can show food items based on category by using these tables?
You are joining Food table and Categories table but there is no relation between them directly. So you have to join these two tables through category_food_restaurant. The following should work:
DB::table('category_food_restaurant')
->select('Food.Food_id','Food.FoodName',
'Food.FoodImage','Categories.CategoryName')
->join('Categories','Categories.Category_id',
'=','category_food_restaurant.Category_id')
->join('Food','Food.Food_id',
'=','category_food_restaurant.Food_id')
->where('Categories.CategoryName',
'=','Breakfast')->get();

Codeigniter placeholder in select statement (select id, 0 as placeholder, ...)

I have query that counts ids in joined table. I need to do an union of returned table with another table in database.
The table returned from first query has 4 columns:
-group_id
-count
-name
-description
The table I want to unite with the first one has 3 columns:
-group_id
-name
-description
I run query through phpmyadmin and it worked perfectly. Query:
SELECT group_id, size, name, description
FROM(SELECT *, count(group_id) as size
FROM table1
GROUP BY group_id) as tmp
JOIN table2
ON tmp.group_id = table2.group_id
UNION
SELECT id, 0 as size, name, description
FROM table2
But when I try to make query with codeigniter, it won't work.
Error: Unknown column '0' in 'field list'
SELECT id, 0 as size, name, description
FROM table2
Here is the codeigniter code from module:
$this->db->select('group_id, size, name, description');
$this->db->from('(select *, count(group_id) as size from table1 group by group_id) as tmp');
$this->db->join('table2', 'tmp.group_id=table2.id');
$this->db->get();
$query1 = $this->db->last_query();
$this->db->select('id, 0 as size, name, description');
$this->db->from('table2');
$this->db->get();
$query2 = $this->db->last_query();
$this->db->query($query1. ' UNION ' .$query2);
$query = $this->db->get();
return $query->result_array();
To make the story short I need to know how to make placeholder with codeigniter or is there any other, better way to get the result I need?
You have to escape your select query by adding FALSE value as the second parameter of codeigniter's active record select() function.
So, it should be written like:
$this->db->select('id, 0 as size, name, description',FALSE);
When you add FALSE value to the second parameter of it, codeigniter won't keep the first parameter to be initialized by backtick character to the query.

Laravel 5 SQLSTATE[42S22]: Column not found

I am doing some joins and trying to get the data. My query builder is:
$datasource = DB::table('vehicles')->join('brands', 'vehicles.brand_id', '=', 'brands.id')->join('sections', 'vehicles.section_id', '=', 'sections.id')->select('vehicles.*, vehicles.id AS vid');
But i am getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'vehicles.model,' in 'field list' (SQL: select vehicles.model, as
AS from vehicles inner join brands on vehicles.brand_id =
brands.id inner join sections on vehicles.section_id =
sections.id limit 4 offset 0) Line 620
What i am doing wrong ?
You should use selectRaw() instead of select():
->selectRaw('vehicles.*, vehicles.id AS vid');
Read more about raw expressions: http://laravel.com/docs/5.0/queries#raw-expressions

Cannot retrieve data from database - Error 1241

Error
SQL query: Documentation
SELECT status.id AS id, status.status
FROM STATUS
WHERE id
IN (
SELECT student.id, student.fname
FROM student,
STATUS WHERE student.id =14
)
ORDER BY status_id DESC
MySQL said: Documentation
#1241 - Operand should contain 1 column(s)
The SELECT inside the IN() should not select more than one column.
in the subuery "SELECT student.id, student.fname" you have to extract only one field

Categories