I'm trying to create a query that will return all the jobs published by a specific company, and a count of the total people who applied to this job. the first part works fine - I get all the jobs and everything I need:
$query = "SELECT *,j.job_id as jid, c.name as city_name ".
"FROM jobs j JOIN areas a ON a.area_id = j.job_area ".
"JOIN positions p ON p.position_id = j.job_position ".
"JOIN fields f ON f.id = j.job_field ".
"JOIN cities c ON j.job_city = c.id ".
"JOIN jobTypes jt ON j.job_type = jt.job_id " .
"JOIN companies comp ON j.job_company = comp.company_id ".
"LEFT JOIN jobApplications ja ON ".
"ja.user_id = '".$_SESSION['user_id']."' AND ".
"j.job_id = ja.job_id WHERE j.job_company='$company_id'";
The thing is, that I want to add each result row the number of applicants for the job from the jobApplications table... I tried to add a COUNT column to the query, which works great by itself:
SELECT COUNT(*) FROM jobApplications ja WHERE ja.job_id=j.job_id
when added to the first big query, I didn't manage to make this work even on the syntax level, so i'm not sure if it works at all...
I tried to add the last query to the select area of the main query, but I always get a syntax error right after the 'ja.job_id=j.job_id' in the end of the count query...
Is this even possible ?
I hope the question is clear, I know there are many tables included here...
Thanks for the time and help!
i dont know your PK of jobApplications, but this might work.
$query = "SELECT *,j.job_id as jid, c.name as city_name, COUNT(ja.<primary key>) ".
"FROM jobs j JOIN areas a ON a.area_id = j.job_area ".
"JOIN positions p ON p.position_id = j.job_position ".
"JOIN fields f ON f.id = j.job_field ".
"JOIN cities c ON j.job_city = c.id ".
"JOIN jobTypes jt ON j.job_type = jt.job_id " .
"JOIN companies comp ON j.job_company = comp.company_id ".
"LEFT JOIN jobApplications ja ON ".
"ja.user_id = '".$_SESSION['user_id']."' AND ".
"j.job_id = ja.job_id WHERE j.job_company='$company_id' ".
"GROUP By jid";
You have to use GROUP
SELECT *,j.job_id as jid, c.name as city_name, COUNT(jobApplications.*)
FROM jobs j JOIN areas a ON a.area_id = j.job_area
JOIN positions p ON p.position_id = j.job_position
JOIN fields f ON f.id = j.job_field
JOIN cities c ON j.job_city = c.id
JOIN jobTypes jt ON j.job_type = jt.job_id
JOIN companies comp ON j.job_company = comp.company_id
LEFT JOIN jobApplications ja ON
ja.user_id = '".$_SESSION['user_id']."' AND
j.job_id = ja.job_id WHERE j.job_company='$company_id'
GROUP BY jid
Hope it helps :)
Try it like this:
$query = "SELECT (SELECT COUNT(ja2.job_id) FROM jobApplications ja2 WHERE ja2.job_id=j.job_id group by j.job_id), *,j.job_id as jid, c.name as city_name FROM jobs j JOIN areas a ON a.area_id = j.job_area" .
" JOIN positions p ON p.position_id = j.job_position JOIN fields f ON f.id = j.job_field "
." JOIN cities c ON j.job_city = c.id JOIN jobTypes jt ON j.job_type = jt.job_id " .
"JOIN companies comp ON j.job_company = comp.company_id LEFT JOIN jobApplications ja ON ja.user_id = '".$_SESSION['user_id']."' AND j.job_id = ja.job_id WHERE j.job_company='$company_id'";
Related
I am trying to add below query in $this_select with left join but not working properly
Below is my working query which works fine :
select a.id_customer as id_customer,
a.id_shop,
a.email,
a.lastname,
a.firstname,
max(c.date_add) as last_visit,
IFNULL(max(b.date_add),'1001-01-01 00:00:00') as Last_order_date
from ps_customer a
left join ps_orders b
on a.id_customer = b.id_customer
left join ps_guest g
on a.id_customer = g.id_customer
left join ps_connections c
on g.id_guest = c.id_guest
group by a.id_customer
having to_days(Last_order_date) < to_days(now())- '30'
But my problem is that when I placed below query code in my controller it is not taking the first and the second left join:
$this->_select='
a.id_shop,
a.email,
a.lastname,
a.firstname,
max(c.date_add) as last_visit,
IFNULL(max(b.date_add),"'.$default_date.'") as Last_order_date
';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'orders` b ON (a.`id_customer` =b.`id_customer`)';
$this->_join ='left join ps_guest g
on (a.id_customer = g.id_customer)';
$this->_join ='left join ps_connections c
ON ( g.id_guest = c.id_guest)
group by a.id_customer
having to_days(Last_order_date) < to_days(now())- '.$dormant_filter_days.'';
Am I doing anything wrong in the above $this_select or $this_join ??
Bleow is db exception which I get the problem is that I am not seeing my first two joins here ie it is not taking the first two joins
You're overriding the _join value on each call to $this->_join =. You should use $this->_join .= for the second and last join.
$this->_select = '
a.id_shop,
a.email,
a.lastname,
a.firstname,
MAX(c.date_add) AS last_visit,
IFNULL(MAX(b.date_add), "' . $default_date . '") AS Last_order_date';
$this->_join = 'LEFT JOIN `' . _DB_PREFIX_ . 'orders` b
ON (a.`id_customer` = b.`id_customer`)';
$this->_join .= ' LEFT JOIN `' . _DB_PREFIX_ . 'guest` g
ON (a.id_customer = g.id_customer)';
$this->_join .= ' LEFT JOIN `' . _DB_PREFIX_ . 'connections` c
ON (g.id_guest = c.id_guest)';
$this->_group = 'GROUP BY a.id_customer';
$this->_having = 'HAVING TO_DAYS(Last_order_date) < TO_DAYS(NOW()) - ' . $dormant_filter_days;
I tried this way which worked for me :
$this->_select='
a.id_shop,
a.email,
a.lastname,
a.firstname,
a.date_add as registered_date,
g.id_customer as guest_id,
max(c.date_add) as last_visit,
IFNULL(max(b.date_add),"'.$default_date.'") as Last_order_date
';
$this->_join = '
LEFT JOIN `'._DB_PREFIX_.'orders` b ON (a.`id_customer` =b.`id_customer`)
LEFT JOIN ps_guest g on (a.id_customer = g.id_customer)
LEFT JOIN ps_connections c
ON ( g.id_guest = c.id_guest)
';
$this->_where = 'group by a.id_customer having to_days(Last_order_date) < to_days(now())- '.$dormant_filter_days.' AND to_days(a.date_add) < to_days(now())- '.$dormant_filter_days.' ';
$this->_orderBy = 'id_customer';
$this->_orderWay = 'DESC';
I am trying to execute this query to print data from three tables. But somehow the query is not being executed.
SELECT l.locality_id, c.city_name, l.locality_name, cloud_site_location_outlet_localities.minimum_order, cloud_site_location_outlet_localities.delivery_time, cloud_site_location_outlet_localities.delivery_charge FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c on l.city_id=c.city_id
The error that I get on executing above query is:
1054 - Unknown column 'cloud_site_location_outlet_localities.minimum_order' in 'field list'
However this query worked fine when I had to display data from two tables only.
SELECT l.locality_id, c.city_name, l.locality_name FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c on l.city_id=c.city_id
Where am I going wrong?
When I use the following query to export it in CSV, I get redundant data. Out of 1000 entries, I only get 400 and then those 400 are repeated.
This is code I am implementing
$sql = "SELECT l.locality_id, c.city_name, l.locality_name, o.minimum_order, o.delivery_time, o.delivery_charge FROM `cloud_site_location_localities` l INNER JOIN `cloud_site_location_cities` c ON l.city_id = c.city_id INNER JOIN `cloud_site_location_outlet_localities` o ON l.locality_id = o.locality_id";
$result = $DB->query($sql);
//$output = fopen($filename, 'c+');
$output = fopen('php://output', 'c+');
fputcsv($output, array('Location Id', 'City Name', 'Location Name','Minimum Order Amount','Delivery Time','Delivery Charge'));
$row = array();
$csv_output = "";
$csv_output .= "Locality Id,City Name,Locality Name,Minimum Order Amount,Delivery Time,Delivery Charge\n";
foreach($result as $locality_value)
{
$row['locality_id'] = $locality_value->locality_id;
$row['city_name'] = $locality_value->city_name;
$row['locality_name'] = $locality_value->locality_name;
$row['minimum_order'] = $locality_value->minimum_order;
$row['delivery_time'] = $locality_value->delivery_time;
$row['delivery_charge'] = $locality_value->delivery_charge;
$csv_output .= trim($row['locality_id']).",".trim($row['city_name']).",".trim($row['locality_name']).",".trim($row['minimum_order']).",".trim($row['delivery_time']).",".trim($row['delivery_charge']).",,\n";
fputcsv($output, $row);
}
fclose($output);
print $csv_output;
You are missing the join clause for cloud_site_location_outlet_localities. E.g.:
SELECT l.locality_id,
c.city_name,
l.locality_name,
o.minimum_order,
o.delivery_time,
o.delivery_charge
FROM `cloud_site_location_localities` l
INNER JOIN `cloud_site_location_cities` c
ON l.city_id = c.city_id
INNER JOIN `cloud_site_location_outlet_localities` o
ON l.outlet_id = c.outlet_id --just guessing the column names
simple structure for joining three tables in mysql
select *
from
tbA a
inner join
tbB b
on a.common = b.common
inner join
tbC c
on b.common = c.common
How can I implement search function:
where sub_menu_name like '%".$kws."%'
into my working one:
SELECT DISTINCT p.id, p.sub_menu_id, p.sub_menu_name, m.image_id, i.file_url, m.default_menu_id, p.restaurant_id, p.status, p.sub_menu_price
FROM sub_sub_menu AS p
INNER JOIN menu AS m ON m.default_menu_id = p.sub_menu_id
OR m.id = p.sub_menu_id
INNER JOIN icon AS i ON i.id = m.image_id
WHERE p.restaurant_id = '" . (int) $_SESSION['uid'] . "' "
Thanks for any help!
SELECT DISTINCT p.id, p.sub_menu_id, p.sub_menu_name, m.image_id, i.file_url, m.default_menu_id, p.restaurant_id, p.status, p.sub_menu_price
FROM sub_sub_menu AS p
INNER JOIN menu AS m ON m.default_menu_id = p.sub_menu_id
OR m.id = p.sub_menu_id
INNER JOIN icon AS i ON i.id = m.image_id
WHERE p.restaurant_id = '" . (int) $_SESSION['uid'] . "'
AND sub_menu_name LIKE '%".$kws."%'
I think this is what you want.
But I wonder why you have ". and ." before and after $kws. It's meant for concatenation so it won't work in SQL. If you want to check for $kws IN a value, the % % are enough.
I have code:
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid";
$query .= " WHERE a.published = 1
AND a.access <= {$aid}
AND a.trash = 0
AND c.published =
AND c.access <= {$aid}
AND c.trash = 0";
I would like to add a third table ('__some_table') for the parts of the query where a.publish, a.access and a.trash. In other words, I want these fields to be retrieved from another table, not "#__table_one", but I do not know how to incorporate the #__some_table into the current query
I imagine the JOIN command can help me, but I do not know how to code mysql
//not tested
$query = "SELECT a.*, c.name as categoryname, c.id as categoryid
FROM #__table_one as a
LEFT JOIN #__table_two c ON c.id = a.catid
LEFT JOIN #__table_three d ON d.id = a.some_id";
I have a mysql table jobs.
This is the basic structure of jobs.
id
booked_user_id
assigned_user_id
I then also have another table, meta.
Meta has the structure:
id
user_id
first_name
last_name
Here is my php code
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS job_id, job_name, priority_id, meta.first_name, date_booked
FROM jobs
LEFT JOIN (meta) on (meta.user_id = jobs.booked_user_id)
LEFT JOIN (jobs_priorities) on (jobs_priorities.id = jobs.priority_id)
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query($sQuery);
while ( $aRow = mysql_fetch_assoc( $rResult ) )
{
$sOutput .= '"'.addslashes($aRow['job_id']).'",';
}
How can I join these tables so that both booked_user_id and assigned_user_id can access meta.first_name?
When I try
$sOutput .= '"'.addslashes($aRow['first_name']).'",
nothing happens
Thanks for your advice
Tim
You can join twice:
SELECT j.id, b.first_name, a.first_name
FROM jobs j
JOIN meta b ON j.booked_user_id = b.user_id
JOIN meta a ON j.assigned_user_id = a.user_id
Nathan did the fix, but will apply it to your current SQL so you can understand it more
Lets transform your query into this:
SELECT SQL_CALC_FOUND_ROWS job_id, job_name, priority_id, date_booked
FROM jobs j
LEFT JOIN meta b ON b.user_id = j.booked_user_id
LEFT JOIN meta a ON a.user_id = j.assigned_user_id
LEFT JOIN jobs_priorities jp ON jp.id = j.priority_id
$sWhere
$sOrder
$sLimit
What I did is to use alias to method and join twice the meta, (just like what nathan did), I temporarily removed the first_name field,
Then let's add something on the SELECT so you can display both first_name
SELECT SQL_CALC_FOUND_ROWS job_id, job_name, priority_id, date_booked, b.first_name as booked_first_name, a.first_name as assigned_first_name
FROM jobs j
LEFT JOIN meta b ON b.user_id = j.booked_user_id
LEFT JOIN meta a ON a.user_id = j.assigned_user_id
LEFT JOIN jobs_priorities jp ON jp.id = j.priority_id
$sWhere
$sOrder
$sLimit
Now, we added the column booked_first_name and assigned_first_name, now you can call it on your php code like this:
$aRow['booked_first_name'] or $aRow['assigned_first_name']