Counting multiple specific value in the same mysql request - php

Is it possible to do something like this:
SELECT *,
COUNT(* WHERE media_id = 1) as count_1,
COUNT(* WHERE media_id = 2) as count_2
WHERE user_id = 1
or do I have to make separate queries?
Thanks.

You can use conditional sum something as
select
sum(media_id = 1) as count_1,
sum(media_id = 2) as count_2
from table_name
where user_id = 1;
Note that using aggregate function does not make sense with select *

Yup, it's possible.
Try this for example:
select
(select count(*) from TableA where 1=1) as totalA,
(select count(*) from TableB where 1=1) as totalB,
(select count(*) from TableC where 1=1) as totalc

Related

passing results of a query into another query

I need to run a query like below
SELECT `loginname`, `id`, `locality`, `wherefrom`
FROM `Volley` JOIN `register` ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%'
from this result i need to take id and pass it to the other query to check that id exist in another table like below.
SELECT IF(EXISTS(SELECT *
FROM `likes`
WHERE Volleyid = '20'),1,0)
Can i combine this into a single query? For the first one i may get more that 1 rows. I tried and got the results of first query into a array but i am not able to get that particular value from array.
$data = array();
for ($x = 0; $x < mysqli_num_rows($query1); $x++)
{
$data[] = mysqli_fetch_assoc($query1);
}
SELECT IF(EXISTS(SELECT * From
(
SELECT `loginname`,
`id`,
`locality`,
`wherefrom`,
Volleyid
FROM `Volley`
JOIN `register`
ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%'
) AS T1
WHERE Volleyid = '20'),1,0)
You can use a subquery with IN clause (i think what you what it ś that Volleyid it's equal to id):
SELECT IF(EXISTS(SELECT *
FROM `likes`
WHERE Volleyid IN
(SELECT `id`
FROM `Volley` JOIN `register` ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%')),1,0)
If you want to get loginname, id, locality, wherefrom columns in the first query alongside with the 1 or 0 flag checked against table likes in single result set. You can write query that looks like this:
SELECT v.*, IF(l.Volleyid IS NOT NULL, 1, 0) as existFlag
FROM
(SELECT `loginname`, `id`, `locality`, `wherefrom`
FROM `Volley`
JOIN `register` ON loginname=username
WHERE CONCAT_WS('', wherefrom,locality) LIKE '%$search%') AS v
LEFT JOIN
(SELECT DISTINCT Volleyid
FROM `likes`) l ON v.id = l.Volleyid;

compare rows of two query set result in same table

I have table where I store data on basis of date.
Now I need to check that: is there any difference between data of rows with two different dates.
In a simple way you can say that I have two queries which select data from same table now I have to compare each row and column value. for example my two query are -
SELECT * FROM `national` WHERE `upload_date` = '2015-08-04' // return 106 rows
and
SELECT * FROM `national` WHERE `upload_date` = '2015-08-01' // return 106 rows
I have tried to compare this with below query but the result not seem to be correct to me, I am not satisfy with this.
Select U.* from
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.* FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1
Can Any one please provide me correct query ?? thank you
Try this
(
SELECT t1.*
FROM
`national` t1, `national` t2
where
t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
-- put your columns here that you want to compare for same DATA
-- like t1.name=t2.name and etc...
)
You can try with something like that
SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id

Laravel query builder - Union All with Skip and Take

I need to get data from a table using this query:
(select columns
from tableX
join tableZ
on tableX.id1 = tableZ.other_id)
union all
(select columns
from tableX
join tableZ
on tableX.id2 = tableZ.other_id)
LIMIT num1, num2
Without the LIMIT, I get the correct result with my query builder that look like this (let $first be the first query of select and $second is the other select query):
$first->unionAll($second)->get();
When I try to put skip and/or take, the result is not the same as the first query above.
$first->unionAll($second)->take(num1)->skip(num2)->get();
The result query from the above builder (I got it from DB::getQueryLog()) is something like:
(select columns
from tableX
join tableZ
on tableX.id1 = tableZ.other_id LIMIT num1, num2)
union all
(select columns
from tableX
join tableZ
on tableX.id2 = tableZ.other_id)
which ofcourse yield incorrect result. Does anyone knows what is the work around to make the first query? Thanks!
You need to wrap $firstQuery in another query, like this:
$first->unionAll($second);
$rows = DB::table( DB::raw("({$first->toSql()}) as t") )
->mergeBindings($first->getQuery())
->take(num1)->skip(num2)
->get();
This will result in the following query:
select * from (FIRST_QUERY union all SECOND_QUERY) as t limit num1, num2
One Workaround is to use raw query using DB::select and DB::raw in below sample block...
$num1 = 100; // skip first 100 rows
$num2 = 2; // take only 2 rows
$qry_result = DB::select(DB::raw("select * from
(
(select columns from tableX join tableZ on tableX.id1 = tableZ.other_id)
union all
(select columns from tableX join tableZ on tableX.id2 = tableZ.other_id)
) Qry LIMIT :vskip , :vlimit ") , array('vskip'=>$num1,'vlimit'=>$num2)
);

SQL: Check if value is available in tbl1 or tbl2

I want to create a sub-account system (PHP & MYSQL).
I have a user table (users) and a sub users table (sub_users).
How can check if the user is available in the user table, or in the sub users table?
My code:
SELECT DISTINCT *
FROM users
WHERE userid = "steven"
OR WHERE EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = "steven");
ERROR: Check your syntax near "steven"
Also tried:
SELECT *
FROM users
LEFT JOIN sub_users
ON sub_users.user_userid = users.userid
WHERE users.userid = 'steven'
OR sub_users.userid = 'steven'
Same error.
In the first place, you only want one where clause. I would also use single quote instead of double quotes:
SELECT DISTINCT *
FROM users
WHERE userid = 'steven' or
EXISTS (SELECT *
FROM sub_users
WHERE sub_users.userid = 'steven'
);
I doubt you need the distinct keyword, if you are fetching all the columns from users.
Your second query looks ok. Are you sure you are not running the first query twice?
EDIT:
I'm trying to figure out what you want to return. The following returns 1 if 'steven' appears in either table and 0 otherwise:
select (case when exists (select 1 from users where users.userid = 'steven') and
exists (select 1 from sub_users where sub_users.userid = 'steven')
then 1
else 0
end);
This method saves on the overhead of a join and will readily take advantage of indexes on users(userid) and sub_users(userid).
You could also use a union:
(SELECT userid FROM users WHERE userid = 'steven')
UNION
(SELECT userid FROM sub_users WHERE userid = 'steven');
select * from
(select * from users) x,
(select * from sub_users) y
where x.user_id = 'steven' or y.user_id = 'steven'
Good luck !!!

How to get row count (from other table) using subquery in propel?

I want to do something like this:
SELECT a.*, (SELECT COUNT(*) FROM b where b.col1 = a.col2) as count FROM a
is it possible using propel criteria or not?
$c = new Criteria();
$c->addSelectColumn(aPeer::TABLE_NAME.'.*');
$c->addAsColumn('count', '(SELECT COUNT(*) FROM b WHERE b.col1 = a.col2)');
aPeer::doSelect($c);

Categories