In my query I want to check if the users with the specified permissions exists or not. Presently I am using the below query. It will retrieve the rows of authorised users with the specified conditions.
select a.ixUserAuthorization from tblechecklistuserroleassignmentxref r
inner join tblechecklistuserrole u on u.ixUserRole=r.ixUserRole and u.sname='Initiator'
inner join tblechecklistuserauthorization a on a.ixUserAuthorization=r.ixUserAuthorization
and a.ixcustomer='1'and a.ixprogram='1'and a.ixworkpackage='1'and a.ixactivity='1' and a.ixUser='626e28e8-e67a-4d11-8d2c-129d0ab79e96';
If any rows are returned i want to display the result as true ,If no rows are returned from the above query I want to display the result as false.
How can I modify to obtain the result as true or false .
SELECT CASE
WHEN Count(*) >= 1 THEN 'true'
ELSE 'false'
END AS Answer
FROM (SELECT a.ixUserAuthorization
FROM tblechecklistuserroleassignmentxref r
INNER JOIN tblechecklistuserrole u
ON u.ixUserRole = r.ixUserRole
AND u.sname = 'Initiator'
INNER JOIN tblechecklistuserauthorization a
ON a.ixUserAuthorization = r.ixUserAuthorization
AND a.ixcustomer = '1'
AND a.ixprogram = '1'
AND a.ixworkpackage = '1'
AND a.ixactivity = '1'
AND a.ixUser = '626e28e8-e67a-4d11-8d2c-129d0ab79e96') a
use function mysql_num_rows
ex:
if(mysql_num_rows($result))
echo "true";
else
echo "false";
where $result is the result of your query
Related
In my query below I am not getting the desired result. I want the results to sort according to the latest event occurrence based on two different date fields of two different tables. Let's see the query first.
SELECT R1.swp_to, R1.swp_type, R1.swp_date, M.mem_fname, M.mem_lname, M.mem_last_activity, DP.dp_photo, GREATEST(R1.swp_date, R2.swp_date) FROM swipes AS R1
LEFT JOIN swipes AS R2 ON(R1.swp_to = R2.swp_by AND R2.swp_to = R1.swp_by AND R2.swp_type <> 'left')
LEFT JOIN members AS M ON(R1.swp_to = M.mem_id)
LEFT JOIN display_photos AS DP ON(R1.swp_to = DP.dp_mem AND DP.dp_index = 1)
LEFT JOIN messages as MSG ON ((R1.swp_to = MSG.msg_from OR R1.swp_to = MSG.msg_to) AND (R1.swp_by = MSG.msg_from OR R1.swp_by = MSG.msg_to))
WHERE R1.swp_by = :mem AND R2.swp_by IS NOT NULL AND R1.swp_type <> 'left'
ORDER BY IF(MSG.msg_time IS NULL, 0, 1), R1.swp_date
Here in the ORDER BY statement we can see that there are two TIME fields msg_time and swp_date. Whenever there is a new match swp_date updates and when there is a new message msg_time updates. The records fetched using this query must be sorted as per the latest event occurrence (whichever date is the earliest of the two). My current ORDER BY statements does not fulfill the requirement. What am I missing here?
You can use the function GREATEST():
ORDER BY GREATEST(COALESCE(MSG.msg_time, R1.swp_date), R1.swp_date) DESC
This is my query.
select sp.name, spi.sku, sum(spi.price), count(sp.name), spi.in_stock, spi.price, sp.is_published, spi.is_reorderable
from shop_product_category spc
inner join shop_product_category_xref spcx
on spc.shop_product_category_id = spcx.shop_product_category_id
inner join shop_product sp
on sp.shop_product_id = spcx.shop_product_id
inner join shop_product_item spi
on spi.shop_product_id = sp.shop_product_id
inner join shop_order_item soi
on spi.shop_product_item_id = soi.shop_product_item_id
where (spc.shop_product_category_id in (1316))
and sp.is_published = 1
group by sp.name
order by count(sp.name) desc
It returns everything just fine EXCEPT I want it to return lines where sp.is_published = 1 Hence the where clause. Yet to get the desired output, I have to change the current where clause to
sp.is_published <> 1
So the questions is this, why does <> 1 return rows with ones, and =1 return rows with zeros. Thanks
If sp.is_published = 1 is a string, you'll need to put it in ticks IE sp.is_published = '1'
I have a table called payment plan which has this different fields.
I wrote a query to get this rows but I need to modify the query in a way that when I give it a criteria that the amount is 80, which comes from a input field in the html it will only show me one field, from the table, if a I write the amount is 90 it still should show me one table but if I write the amount is 160 or 160 + it should show me both the table. This amounts are basically the CAPITAL_PAYMENT of the table. But I am not sure how to write the query. I wrote this
SELECT c.* , a.Interest
FROM investment a
inner join money_offer b
on a.ORIG_ID = b.investment_orig_id and b.UPDATE_DT is null
inner join payment_plan c
on c.offer_orig_id = b.ORIG_ID and c.UPDATE_DT is null
WHERE a.ORIG_ID = 21 and
a.Owner = 533 and
a.UPDATE_DT is null;
Now I get two rows but depending on amount, if 90, I should get 1 row but I am not sure where do I write it or how.
This amount is basically coming from codeigniter function if it helps here is the code.
public function getLoansBorrowedData($id, $orig_id , $amount){
$query = 'SELECT c.* , a.Interest , d.symbol
FROM investment a
inner join money_offer b
on a.ORIG_ID = b.investment_orig_id and b.UPDATE_DT is null
inner join payment_plan c
on c.offer_orig_id = b.ORIG_ID and c.UPDATE_DT is null
inner join currency d
on c.currency = d.ID
WHERE a.ORIG_ID = '.$orig_id.' and
a.Owner = '.$id.' and
a.UPDATE_DT is null' ;
$query = $this->db->query($query);
/* Return either found rows or false. */
if($query->num_rows() > 0){
$result = $query->result();
return $result;
}
else {
return false;
}
}
The below runs fine :
select Runs.runId,Runs.prodId,fail,Owner,
(cast(counts.Count as decimal(4,2))) as PercentAnalysed,Language,Platform
from Runs AS Runs Inner Join Product AS Product On Runs.prodId=Product.prodId
left join (SELECT COUNT(*) AS 'Count', Results.runId
FROM Results inner join Runs on Results.runId=Runs.runId
WHERE Analysed = 'True'
GROUP BY Results.runId ) counts on counts.runId = Runs.runId
I want PercentAnalysed value in outer query to be as below :
= [counts.Count/failCount] if failCount !=0
or
=[0] if failCount=0
How do I do this ?
Any idea anyone ?
You need to use the CASE statement to get what you want.
SELECT
Runs.runId,
Runs.prodId,
fail,
Owner,
CAST(CASE WHEN failcount <> 0 THEN counts.Count/failcount
WHEN failcount = 0 OR counts.Count IS NULL THEN 0
ELSE counts.Count END AS Decimal(4,2)) PercentAnalysed,
Language,
Platform
FROM
.....
I'm using a nested query to get the value I need from a table that I then need to use in a conditional statement, however, every time I try this I keep on getting an error saying unknown column (format) in the field list
SELECT
(SELECT format FROM competition_stages WHERE comp_id = "5" AND rid = "24") AS format,
a.tie_id, b.name AS team_a, b.team_id AS team_a_id, c.name AS team_b, c.team_id AS team_b_id, SUM(e.bonus) AS team_a_bonus, SUM(f.bonus) AS team_b_bonus,
SUM(CASE
WHEN (a.team_a = e.team_id AND format = "0") THEN e.score
END) as team_a_agg,
SUM(CASE
WHEN (a.team_b = f.team_id AND format = "0") THEN f.score
END) as team_b_agg
FROM competition_tie a
INNER JOIN teams b ON (a.team_a = b.team_id)
INNER JOIN teams c ON (a.team_b = c.team_id)
LEFT JOIN fixtures d ON (a.tie_id = d.tie_id)
LEFT JOIN fixture_scores e ON (d.fx_id = e.fx_id AND a.team_a = e.team_id)
LEFT JOIN fixture_scores f ON (d.fx_id = f.fx_id AND a.team_b = f.team_id)
WHERE a.comp_id = "5" AND a.rid = "24" AND a.season_id = "5"
GROUP BY a.tie_id
ORDER BY a.tie_id ASC
I can get the value of the format column in my results when I go through them but it just seems I can't use it within my query to use in.
Thanks for your help!
Instead of using a subquery, simply join the competition_stages table to your query so that you can reference the format column directly. Assuming there is no appearant relation between the competition_stages table and the other tables in the query (at least not from the information on hand), you can just join the table using the two conditions you specified if these conditions would only yield 1 result in the competition_stages table. Something like this:
SELECT cs.format, a.tie_id, ....
FROM competition_tie a ...
INNER JOIN competition_stages cs ON cs.comp_id = "5" AND cs.rid = "24"