I am currently trying to pass through a variable to act like a where clause so I can pass it to the URL and display content for each event on a page.
To do this, I'd need to pass the event_id.
public function exportSearch($event_id){
$query = "SELECT
u.prefix AS 'Title',
u.forename AS 'Forename',
u.surname AS 'Surname',
u.telephone AS 'Mobile Number',
u.email AS 'Email Address',
ea.checkin_status_id AS 'checked in',
u.update_time AS 'Register Date',
eg.name AS 'Event Group Registered',
e.name AS 'Session Registered',
u.bio AS 'User Information',
u.dob AS 'Date of Birth',
u.company AS 'Company',
u.company_role AS 'Company Role',
CONCAT(u2.forename, ' ', u2.surname) AS 'Guest of',
MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) AS Question1,
MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) AS Question2,
MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) AS Question3,
MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) AS Question4,
MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) AS Question5,
MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) AS Question6
FROM tbl_event_attendees AS ea
LEFT JOIN tbl_user AS u ON ea.user_id = u.id
LEFT JOIN tbl_event AS e ON ea.event_id = e.id
LEFT JOIN tbl_event_groups AS eg on e.group_id = eg.id
LEFT JOIN tbl_user AS u2 ON ea.guest_of_user_id = u2.id
LEFT JOIN tbl_user_variables AS uv on u.id = uv.user_id
GROUP BY ea.id";
// $query->params([':event_id' => $event_id]);
$count=Yii::app()->db->createCommand($query)->queryScalar();
return $sqlDataprovider = new CSqlDataProvider($query, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
// 'event_name'=>Event::model()->getAttributeLabel('name'), // csv of sortable column names
//'Question1' => EventAttendees::model()->getAttributeLabel('Question1'),
// 'Forename',
)
),
'pagination'=>array(
'pageSize'=>$count, //Show all records
),
));
}
My idea was to use $query->params to do this and then throw it over to the view but had no luck.
It needs to display something like WHERE ea.event_id = $event_id but I can't seem to pass it through via the raw sql.
You need to add WHERE clause in your SQL like that:
WHERE ea.event_id = :event_id. Remember, your $query variable only contains SQL, so this is a string, not a something-like query object.
To pass parameter to queryScalar() function you need to pass an array which contains params. According to queryScalar() documentation:
$count = Yii::app()->db->createCommand($query)->queryScalar(array(
':event_id'=>$event_id
));
NOTE:
Documentation says:
Executes the SQL statement and returns the value of the first column
in the first row of data. This is a convenient method of query when
only a single scalar value is needed (e.g. obtaining the count of the
records).
So, your first column is u.prefix AS 'Title' is string. Using it as $count its not a good solution. I suggest to write one SQL for getting count of rows, and other for getting data that will be passed to CSqlDataProvider.
The sql should be this
"SELECT
u.prefix AS 'Title',
u.forename AS 'Forename',
u.surname AS 'Surname',
u.telephone AS 'Mobile Number',
u.email AS 'Email Address',
ea.checkin_status_id AS 'checked in',
u.update_time AS 'Register Date',
eg.name AS 'Event Group Registered',
e.name AS 'Session Registered',
u.bio AS 'User Information',
u.dob AS 'Date of Birth',
u.company AS 'Company',
u.company_role AS 'Company Role',
CONCAT(u2.forename, ' ', u2.surname) AS 'Guest of',
MAX(CASE WHEN uv.user_type_variables_id = 1 THEN uv.value ELSE NULL END) AS Question1,
MAX(CASE WHEN uv.user_type_variables_id = 2 THEN uv.value ELSE NULL END) AS Question2,
MAX(CASE WHEN uv.user_type_variables_id = 3 THEN uv.value ELSE NULL END) AS Question3,
MAX(CASE WHEN uv.user_type_variables_id = 4 THEN uv.value ELSE NULL END) AS Question4,
MAX(CASE WHEN uv.user_type_variables_id = 5 THEN uv.value ELSE NULL END) AS Question5,
MAX(CASE WHEN uv.user_type_variables_id = 6 THEN uv.value ELSE NULL END) AS Question6
FROM tbl_event_attendees AS ea
LEFT JOIN tbl_user AS u ON ea.user_id = u.id
LEFT JOIN tbl_event AS e ON ea.event_id = e.id
LEFT JOIN tbl_event_groups AS eg on e.group_id = eg.id
LEFT JOIN tbl_user AS u2 ON ea.guest_of_user_id = u2.id
LEFT JOIN tbl_user_variables AS uv on u.id = uv.user_id
WHERE ea.id = " . $event_id . "
GROUP BY ea.id"
Related
I am using join in laravel with mutiples and different conditions but I have a problem the quantity and the date column is always null but when I see in the database the column is not null it has some value it is only null when I am using join.
$contact_packages = Contact::where('contacts.id','!=',1)
->leftJoin('transactions as t','t.id','=','contacts.id')
->leftJoin('transaction_sell_lines as tsl','tsl.transaction_id','=','t.id')
->join('customer_package as cp','contacts.id','=','cp.contact_id')
->join('package as p','cp.package_id','=','p.id')
->join('customer_package_service as cps','cps.customer_package_id','=','cp.id')
->leftJoin('categories as c','p.category_id','=','c.id')
->leftJoin('categories as sc','p.sub_category_id','=','sc.id')
// ->where('cps.customer_package_id','cp.id')
->select([
'contacts.id as cid',
'contacts.name as cname',
'p.id as pid',
'p.name as pname',
DB::raw('TRIM(tsl.quantity)+0 as pqty'),
// 'tsl.quantity as pqty',
DB::raw('count(cps.product_id) as services'),
DB::raw('COUNT(CASE WHEN cps.status = 0 THEN cps.product_id END) as pending'),
DB::raw('COUNT(CASE WHEN cps.status = 1 THEN cps.product_id END) as redeemed'),
't.created_at as date',
'c.name as pc',
'sc.name as psc',
'p.expire_days as ped',
'cps.customer_package_id as cpid'
])
->groupBy('cpid')
->get();
select `contacts`.`id` as `cid`, `contacts`.`name` as `cname`,
`p`.`id` as `pid`, `p`.`name` as `pname`,
TRIM(tsl.quantity)+0 as pqty, count(cps.product_id) as services,
COUNT(CASE WHEN cps.status = 0 THEN cps.product_id END) as pending,
COUNT(CASE WHEN cps.status = 1 THEN cps.product_id END) as redeemed,
`t`.`created_at` as `date`, `c`.`name` as `pc`,
`sc`.`name` as `psc`, `p`.`expire_days` as `ped`,
`cps`.`customer_package_id` as `cpid` from `contacts`
left join `transactions` as `t` on `t`.`id` = `contacts`.`id`
left join `transaction_sell_lines` as `tsl` on `tsl`.`transaction_id` = `t`.`id`
inner join `customer_package` as `cp` on `contacts`.`id` = `cp`.`contact_id`
inner join `package` as `p` on `cp`.`package_id` = `p`.`id`
inner join `customer_package_service` as `cps` on `cps`.`customer_package_id` = `cp`.`id`
left join `categories` as `c` on `p`.`category_id` = `c`.`id`
left join `categories` as `sc` on `p`.`sub_category_id` = `sc`.`id`
where `contacts`.`id` != ?
and `contacts`.`deleted_at` is null
group by `cpid`
I just had to put the foreign name in first join I put the primary key which is wrong.
$contact_packages = Contact::where('contacts.id','!=',1)
->leftJoin('transactions as t','t.contact_id','=','contacts.id')
->leftJoin('transaction_sell_lines as tsl','tsl.transaction_id','=','t.id')
->join('customer_package as cp','contacts.id','=','cp.contact_id')
->join('package as p','cp.package_id','=','p.id')
->join('customer_package_service as cps','cps.customer_package_id','=','cp.id')
->leftJoin('categories as c','p.category_id','=','c.id')
->leftJoin('categories as sc','p.sub_category_id','=','sc.id')
// ->where('cps.customer_package_id','cp.id')
->select([
'contacts.id as cid',
'contacts.name as cname',
'p.id as pid',
'p.name as pname',
DB::raw('TRIM(tsl.quantity)+0 as pqty'),
// 'tsl.quantity as pqty',
DB::raw('count(cps.product_id) as services'),
DB::raw('COUNT(CASE WHEN cps.status = 0 THEN cps.product_id END) as pending'),
DB::raw('COUNT(CASE WHEN cps.status = 1 THEN cps.product_id END) as redeemed'),
't.created_at as date',
'c.name as pc',
'sc.name as psc',
'p.expire_days as ped',
'cps.customer_package_id as cpid'
])
->groupBy('cpid')
->get();
I have a problem with my queries. I have now 2 queries and that works fine. But when I want to add another query with a PIVOT, it doesn't work. I have tried a lot of things but nothing works..
This is my first two queries
$query = "SET SQL_BIG_SELECTS = 1;";
$query .= "SELECT * FROM datakram, datakram2, datakram3 WHERE datakram.NAME = datakram2.NAME AND datakram2.NAME = datakram3.NAME"
And I want to add a PIVOT for table "datakram4". But I want only the rows where the NAME is equal to the NAME in the others tables. Without the PIVOT it works..
My PIVOT code.
SELECT `name` ,
MAX( CASE WHEN `year` =2017 THEN `income` ELSE 0 END ) AS INCOME_2017,
MAX( CASE WHEN `year` =2017 THEN `expense` ELSE 0 END ) AS EXPENSE_2017,
MAX( CASE WHEN `year` =2016 THEN `income` ELSE 0 END ) AS INCOME_2016,
MAX( CASE WHEN `year` =2016 THEN `expense` ELSE 0 END ) AS EXPENSE_2016
FROM `test_data` GROUP BY `name`
I use multi_query for my php script.
Simply join the queries:
SELECT d4.*
FROM datakram d1
INNER JOIN datakram2 d2
ON d1.`NAME` = d2.`NAME`
INNER JOIN datakram3 d3
ON d2.`NAME` = d3.`NAME`
INNER JOIN
(SELECT `name` ,
MAX(CASE WHEN `year`=2017 THEN `income` ELSE 0 END) AS INCOME_2017,
MAX(CASE WHEN `year`=2017 THEN `expense` ELSE 0 END) AS EXPENSE_2017,
MAX(CASE WHEN `year`=2016 THEN `income` ELSE 0 END) AS INCOME_2016,
MAX(CASE WHEN `year`=2016 THEN `expense` ELSE 0 END) AS EXPENSE_2016
FROM `test_data`
GROUP BY `name`
) d4
ON d3.`NAME` = d4.`name`
A simple way is to filter in the WHERE clause:
SELECT `name` ,
MAX(CASE WHEN `year` = 2017 THEN `income` ELSE 0 END) AS INCOME_2017,
MAX(CASE WHEN `year` = 2017 THEN `expense` ELSE 0 END) AS EXPENSE_2017,
MAX(CASE WHEN `year` = 2016 THEN `income` ELSE 0 END) AS INCOME_2016,
MAX(CASE WHEN `year` = 2016 THEN `expense` ELSE 0 END) AS EXPENSE_2016
FROM `test_data` td
WHERE EXISTS (SELECT 1 FROM FROM datakram d WHERE d.name = td.NAME) AND
EXISTS (SELECT 1 FROM FROM datakram2 d WHERE d.name = td.NAME) AND
EXISTS (SELECT 1 FROM FROM datakram3 d WHERE d.name = td.NAME)
GROUP BY `name` ;
I want to use a table's column value as column name and use it as JOIN ON value with another table column name. Below is my query.
SELECT nid, sid,
MAX(CASE WHEN cid = 2 THEN data END) first_name,
MAX(CASE WHEN cid = 3 THEN data END) last_name,
MAX(CASE WHEN cid = 4 THEN data END) email,
MAX(CASE WHEN cid = 5 THEN data END) phone_number,
MAX(CASE WHEN cid = 6 THEN data END) transitions,
MAX(CASE WHEN cid = 7 THEN data END) **upload**,
MAX(CASE WHEN cid = 9 THEN data END) coupon_code
FROM wsd AS wsd JOIN files AS fm ON **upload** = fm.fid WHERE nid IN(66,67) GROUP BY sid
As you see the above code, I want to use upload column as the JOIN on another table. Also, is it possible to embed HTML tags on the results?
I tried to get the results using temporary table. Here is my answer.
CREATE TEMPORARY TABLE IF NOT EXISTS my_temp_table SELECT sid, cid, data FROM wsd WHERE cid = 7 AND nid IN (66,67);
SELECT wsd.nid, wsd.sid, fm.filename,
MAX(CASE WHEN wsd.cid = 2 THEN wsd.data END) fn,
MAX(CASE WHEN wsd.cid = 3 THEN wsd.data END) ln,
MAX(CASE WHEN wsd.cid = 4 THEN wsd.data END) email,
MAX(CASE WHEN wsd.cid = 5 THEN wsd.data END) ph,
MAX(CASE WHEN wsd.cid = 6 THEN wsd.data END) tr,
MAX(CASE WHEN wsd.cid = 7 THEN wsd.data END) upr,
MAX(CASE WHEN wsd.cid = 9 THEN wsd.data END) coc
FROM wsd wsd JOIN my_temp_table ON wsd.sid = my_temp_table.sid JOIN files fm ON my_temp_table.data = fm.fid WHERE nid IN(66,67) GROUP BY wsd.sid;
Instead of temporary table, better query as follows:
SELECT wsd.nid, wsd.sid, fm.filename,
MAX(CASE WHEN wsd.cid = 2 THEN wsd.data END) fn,
MAX(CASE WHEN wsd.cid = 3 THEN wsd.data END) ln,
MAX(CASE WHEN wsd.cid = 4 THEN wsd.data END) email,
MAX(CASE WHEN wsd.cid = 5 THEN wsd.data END) ph,
MAX(CASE WHEN wsd.cid = 6 THEN wsd.data END) tr,
MAX(CASE WHEN wsd.cid = 7 THEN wsd.data END) upr,
MAX(CASE WHEN wsd.cid = 9 THEN wsd.data END) coc
FROM wsd wsd LEFT JOIN files fm ON wsd.data = fm.fid WHERE nid IN(66,67) GROUP BY wsd.sid;
I am using below mysql query using php to run my report for users
SET #p_yyyy_1 = ".$year[0].";
SET #p_q_1 = '".$quarter[0]."';
SET #p_yyyy_2 = ".$year[1].";
SET #p_q_2 = '".$quarter[1]."';
SET #p_yyyy_3 = ".$year[2].";
SET #p_q_3 = '".$quarter[2]."';
SET #p_yyyy_4 = ".$year[3].";
SET #p_q_4 = '".$quarter[3]."';
select pr.profile_id
, max(pr.fname) fname, max(pr.lname) lname
, max(case when p.year = #p_yyyy_1 and p.quarter = #p_q_1 then p.grade else null end) PPT_Q1
, max(case when p.year = #p_yyyy_2 and p.quarter = #p_q_2 then p.grade else null end) PPT_Q2
, max(case when p.year = #p_yyyy_3 and p.quarter = #p_q_3 then p.grade else null end) PPT_Q3
, max(case when p.year = #p_yyyy_4 and p.quarter = #p_q_4 then p.grade else null end) PPT_Q4
, max(case when b.year = #p_yyyy_1 and b.quarter = #p_q_1 then b.grade else null end) BPET_Q1
, max(case when b.year = #p_yyyy_2 and b.quarter = #p_q_2 then b.grade else null end) BPET_Q2
, max(case when b.year = #p_yyyy_3 and b.quarter = #p_q_3 then b.grade else null end) BPET_Q3
, max(case when b.year = #p_yyyy_4 and b.quarter = #p_q_4 then b.grade else null end) BPET_Q4
, max(case when bo.year = #p_yyyy_1 and bo.quarter = #p_q_1 then bo.grade else null end) BOAC_Q1
, max(case when bo.year = #p_yyyy_2 and bo.quarter = #p_q_2 then bo.grade else null end) BOAC_Q2
, max(case when bo.year = #p_yyyy_3 and bo.quarter = #p_q_3 then bo.grade else null end) BOAC_Q3
, max(case when bo.year = #p_yyyy_4 and bo.quarter = #p_q_4 then bo.grade else null end) BOAC_Q4
, max(case when f.year = #p_yyyy_1 and f.quarter = #p_q_1 then f.grade else null end) FIRING_Q1
, max(case when f.year = #p_yyyy_2 and f.quarter = #p_q_2 then f.grade else null end) FIRING_Q2
, max(case when f.year = #p_yyyy_3 and f.quarter = #p_q_3 then f.grade else null end) FIRING_Q3
, max(case when f.year = #p_yyyy_4 and f.quarter = #p_q_4 then f.grade else null end) FIRING_Q4
from profile_header pr
cross join (
select #p_yyyy_1 yyyy, #p_q_1 q
union select #p_yyyy_2, #p_q_2
union select #p_yyyy_3, #p_q_3
union select #p_yyyy_4, #p_q_4
) yq
left join ppt_header ph on ph.profile_id = pr.profile_id
and ph.delete_flag = 'n'
left join ppt p on ph.ppt_header_id = p.ppt_header_id
and p.year = yq.yyyy
and p.quarter = yq.q
left join bpet_header bh on bh.profile_id = pr.profile_id
and bh.delete_flag = 'n'
left join bpet b on bh.bpet_header_id = b.bpet_header_id
and b.year = yq.yyyy
and b.quarter = yq.q
left join baoc_header boh on boh.profile_id = pr.profile_id
and boh.delete_flag = 'n'
left join baoc bo on boh.baoc_header_id = bo.baoc_header_id
and bo.year = yq.yyyy
and bo.quarter = yq.q
left join firingqt_header fh on fh.profile_id = pr.profile_id
and fh.delete_flag = 'n'
left join firing_qt f on fh.firingqt_header_id = f.firingqt_header_id
and f.year = yq.yyyy
and f.quarter = yq.q
left join lookup l1 on pr.group_id = l1.lookup_id
left join lookup l2 on pr.hit_no = l2.lookup_id
where pr.profile_id IN ($commando)
group by pr.profile_id
ORDER BY 2 ASC, 3 ASC
When I am running this query directly in phpmyadmin query tab, it's working perfectly, but when I run this query through my php file and script I get this mysql error :
MySQL Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'SET #p_q_1 = 'Q1'; SET #p_yyyy_2 = 2014; SET #p_q_2 = 'Q4';
SET #p_y' at line 2
I have a WordPress installation and I'm making a custom plugin. My plugin stores a good bit of usermeta. I'm using an inner join to combine data from the "users" and "usermeta" table at which point I spit the result back to my PHP script. Here's what my query looks like:
select
# Users table stuff
users.ID,
users.user_email,
users.display_name,
# Meta stuff
first_name.meta_value as first_name,
last_name.meta_value as last_name,
phone_number.meta_value as phone_number,
country.meta_value as country,
years_of_experience.meta_value as years_of_experience,
highest_degree_obtained.meta_value as highest_degree_obtained,
availability_for_work.meta_value as availability_for_work,
english_proficiency.meta_value as english_proficiency,
disciplines.meta_value as disciplines,
profile_picture.meta_value as profile_picture,
resume.meta_value as resume,
description.meta_value as description,
hourly_rate.meta_value as hourly_rate,
satisfaction_rating.meta_value as satisfaction_rating,
invited_projects.meta_value as invited_projects,
completed_project_count.meta_value as completed_project_count
# The table we're selecting from
from tsd_retro_users as users
# Join in our usermeta table for each individual meta value
inner join tsd_retro_usermeta first_name on users.ID = first_name.user_id
inner join tsd_retro_usermeta last_name on users.ID = last_name.user_id
inner join tsd_retro_usermeta phone_number on users.ID = phone_number.user_id
inner join tsd_retro_usermeta country on users.ID = country.user_id
inner join tsd_retro_usermeta years_of_experience on users.ID = years_of_experience.user_id
inner join tsd_retro_usermeta highest_degree_obtained on users.ID = highest_degree_obtained.user_id
inner join tsd_retro_usermeta availability_for_work on users.ID = availability_for_work.user_id
inner join tsd_retro_usermeta english_proficiency on users.ID = english_proficiency.user_id
inner join tsd_retro_usermeta disciplines on users.ID = disciplines.user_id
inner join tsd_retro_usermeta profile_picture on users.ID = profile_picture.user_id
inner join tsd_retro_usermeta resume on users.ID = resume.user_id
inner join tsd_retro_usermeta description on users.ID = description.user_id
inner join tsd_retro_usermeta hourly_rate on users.ID = hourly_rate.user_id
inner join tsd_retro_usermeta satisfaction_rating on users.ID = satisfaction_rating.user_id
inner join tsd_retro_usermeta invited_projects on users.ID = invited_projects.user_id
inner join tsd_retro_usermeta completed_project_count on users.ID = completed_project_count.user_id
# Define our select stipulations
where
(users.ID = 20)
and
(first_name.meta_key = 'first_name')
and
(last_name.meta_key = 'last_name')
and
(phone_number.meta_key = 'phone_number')
and
(country.meta_key = 'country')
and
(years_of_experience.meta_key = 'years_of_experience')
and
(highest_degree_obtained.meta_key = 'highest_degree_obtained')
and
(availability_for_work.meta_key = 'availability_for_work')
and
(english_proficiency.meta_key = 'english_proficiency')
and
(disciplines.meta_key = 'disciplines')
and
(profile_picture.meta_key = 'profile_picture')
and
(resume.meta_key = 'resume')
and
(description.meta_key = 'description')
and
(hourly_rate.meta_key = 'hourly_rate')
and
(satisfaction_rating.meta_key = 'satisfaction_rating')
and
(invited_projects.meta_key = 'invited_projects')
and
(completed_project_count.meta_key = 'completed_project_count')
As you can see, I inner join the usermeta table for each value that I'm trying to obtain from the database. It all seems to work fine, but after a certain number of inner joins, the query seems to slow to a crawl. Right now, the above query is taking about a second on average, and I only have about twenty users in my user table.
My question is: what are the performance ramifications of inner joins? Is there a better way to run the above query and achieve the same result?
Try this way:
select
# Users table stuff
users.ID,
users.user_email,
users.display_name,
# Meta stuff
MAX(CASE WHEN meta_table.meta_key='first_name' THEN meta_table.first_name ELSE NULL END) as first_name,
MAX(CASE WHEN meta_table.meta_key='last_name' THEN meta_table.last_name ELSE NULL END) as last_name,
MAX(CASE WHEN meta_table.meta_key='phone_number' THEN meta_table.phone_number ELSE NULL END) as phone_number,
MAX(CASE WHEN meta_table.meta_key='country' THEN meta_table.country ELSE NULL END) as country,
MAX(CASE WHEN meta_table.meta_key='years_of_experience' THEN meta_table.years_of_experience ELSE NULL END) as years_of_experience,
MAX(CASE WHEN meta_table.meta_key='highest_degree_obtained' THEN meta_table.highest_degree_obtained ELSE NULL END) as highest_degree_obtained,
MAX(CASE WHEN meta_table.meta_key='availability_for_work' THEN meta_table.availability_for_work ELSE NULL END) as availability_for_work,
MAX(CASE WHEN meta_table.meta_key='english_proficiency' THEN meta_table.english_proficiency ELSE NULL END) as english_proficiency,
MAX(CASE WHEN meta_table.meta_key='disciplines' THEN meta_table.disciplines ELSE NULL END) as disciplines,
MAX(CASE WHEN meta_table.meta_key='profile_picture' THEN meta_table.profile_picture ELSE NULL END) as profile_picture,
MAX(CASE WHEN meta_table.meta_key='resume' THEN meta_table.resume ELSE NULL END) as resume,
MAX(CASE WHEN meta_table.meta_key='description' THEN meta_table.description ELSE NULL END) as description,
MAX(CASE WHEN meta_table.meta_key='hourly_rate' THEN meta_table.hourly_rate ELSE NULL END) as hourly_rate,
MAX(CASE WHEN meta_table.meta_key='satisfaction_rating' THEN meta_table.satisfaction_rating ELSE NULL END) as satisfaction_rating,
MAX(CASE WHEN meta_table.meta_key='invited_projects' THEN meta_table.invited_projects ELSE NULL END) as invited_projects,
MAX(CASE WHEN meta_table.meta_key='completed_project_count' THEN meta_table.completed_project_count ELSE NULL END) as completed_project_count
# The table we're selecting from
from tsd_retro_users as users
# Join in our usermeta table for each individual meta value
inner join tsd_retro_usermeta as meta_table
ON users.ID = meta_table.user_id
# Define our select stipulations
where
(users.ID = 20)
GROUP BY users.ID
And as soon as we use php you can do this way:
$requiredMetaFields = array(
'first_name',
'last_name',
'phone_number',
'country',
'years_of_experience',
'highest_degree_obtained',
'availability_for_work',
'english_proficiency',
'disciplines',
'profile_picture',
'resume',
'description',
'hourly_rate',
'satisfaction_rating',
'invited_projects',
'completed_project_count'
);
$query = "select
users.ID,
users.user_email,
users.display_name,";
foreach($requiredMetaFields as $metafield) {
$query .= "MAX(CASE WHEN meta_table.meta_key='$metafield' THEN meta_table.$metafield ELSE NULL END) as $metafield, ";
}
$query =substr($query,0,-1);
$query .= "from tsd_retro_users as users
inner join tsd_retro_usermeta as meta_table
ON users.ID = meta_table.user_id
where
(users.ID = 20)
GROUP BY users.ID";
Edited syntax errors