Counting rows from different tables in same query - mysql

I thought i did something like this before:
$reg = mysql_query ("(SELECT count(*) from vouchers) as total_vouchers),
(SELECT count(*) from vouchers WHERE asignado = 1) as vouchers_asignados,
(SELECT count(*) from crm) as crm_users,
(SELECT count(*) from datos_modificados) as dm_users") or die(mysql_error());
But it would return mysql_error near the first , :
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 'as total_vouchers), ((SELECT count(*) from vouchers WHERE asignado = 1) a' at line 1
Any idea why?

Try to add select as below
$reg = mysql_query ("SELECT
(SELECT count(*) from vouchers) as total_vouchers,
(SELECT count(*) from vouchers WHERE asignado = 1) as vouchers_asignados,
(SELECT count(*) from crm) as crm_users,
(SELECT count(*) from datos_modificados) as dm_users") or die(mysql_error());

Related

How to use VIEW in WHERE clause (MySQL)?

I want to use data of view in WHERE clause. But getting an error:
create view post_with_answers AS
SELECT DISTINCT postid
FROM (SELECT postid FROM `qa_posts` WHERE `type` = 'Q') AS q1
INNER JOIN (SELECT parentid FROM `qa_posts` WHERE `type` = 'A') AS q2 ON q1.postid = q2.parentid
select count(*)
from qa_posts
where parentid not in post_with_answers
On the last row I am getting this error:
SQL Error [1064] [42000]: 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 'post_with_answers' at line 3
How to fix that?
Just like you would use a table:
select count(*)
from qa_posts
where parentid not in (select pwa.postid from post_with_answers pwa);
I would caution you from using not in with a subquery. No rows are returned if even one value from the subquery is NULL. For this reason, I recommend NOT EXISTS:
select count(*)
from qa_posts p
where not exists (select 1
from post_with_answers pwa
where p.parentid = pwa.postid
);
In addition, your view definition is a bit over complicated. You don't need subqueries:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq JOIN
qa_posts pa
ON pq.postid = pa.parentid
WHERE pq.type = 'Q' AND pa.type = 'A';
Then, the DISTINCT just adds overhead, so EXISTS is better:
create view post_with_answers AS
SELECT DISTINCT pq.postid
FROM qa_posts pq
WHERE EXISTS (SELECT 1
FROM qa_posts pa
WHERE pq.postid = pa.parentid AND
pa.type = 'A'
)
WHERE pq.type = 'Q';

Error in mysql query with IF statement and sub query

I have (for me at least) a complex query that I build with examples from this site. The last thing I added was the IF statement. Without the IF statement it was working with the TRUE part of the IF statement. I hope you girls/guys can help me out here. This is the query :
SELECT
t.ID, t.start_time, t.end_time, t.start_date, t.end_date, t.balance,
u1.first_name AS teacher_one_first_name, u1.last_name AS teacher_one_last_name,
u2.first_name AS teacher_two_first_name, u2.last_name AS teacher_two_last_name,
company.name, company.post_city, tag, lvl,
IF(
t.balance=1,
(
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE lead_follow=0 AND company_lesson_block_ID=t.ID) AS lead,
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE lead_follow=1 AND company_lesson_block_ID=t.ID) AS follow
),
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE company_lesson_block_ID=t.ID) AS total_student
)
FROM company_lesson_block AS t
LEFT JOIN company_lvl ON company_lvl.ID = t.lvl_ID
LEFT JOIN tag ON tag.ID = t.style_ID
LEFT JOIN company ON company.ID=t.location_ID
LEFT JOIN user AS u1 ON t.teacher_one_ID=u1.ID
LEFT JOIN user AS u2 ON t.teacher_two_ID=u2.ID
WHERE t.company_ID='1' AND location_ID='1' AND company_season_ID='1'
ORDER BY start_date ASC
The error message i get is:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version
for the right syntax to use near 'AS lead, (SELECT count(user_ID) FROM company_less' at line 10
Any help/tips are appriciated
You want to return 2 columns, but IF can return only 1, so you need 2 IFs:
........................
IF(
t.balance=1,
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE lead_follow=0 AND company_lesson_block_ID=t.ID),
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE company_lesson_block_ID=t.ID)
) AS ????,
IF(
t.balance=1,
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE lead_follow=1 AND company_lesson_block_ID=t.ID),
(SELECT count(user_ID)
FROM company_lesson_block_student
WHERE company_lesson_block_ID=t.ID)
) AS ????
........................
You must set the aliases after the closing parenthesis of IF.
Maybe you must rethink this logic because in case of FALSE, the same value is returned twice.

sql error with inner join mysql

String sql = "set #row \\:=-1; SELECT ue.id, ue.Latitude, ue.Longitude, ue.Serving_Cell, ue.RSCP
FROM ue INNER JOIN(
SELECT id
from (
SELECT #row \\:=#row+1 as rownum, id
from (
select id from ue order by id) as sorted
)as ranked
where rownum %20=0) as subset
on subset.id = ue.id
where Operator like :operator ";
Query query = getSessionFactory().getCurrentSession().createSQLQuery(sql);
query.setParameter("operator", "%" + operator + "%");
return query.list();
help me please to find my sql error
console render me :
GRAVE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/SpringSecurity] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'SELECT ue.id, ue.Latitude, ue.Longitude, ue.Serving_Cell, ue.RSCP FROM ue INNER ' at line 1
You try to execute 2 queries at once. You can only execute one at a time.
SELECT ue.id, ue.Latitude, ue.Longitude, ue.Serving_Cell, ue.RSCP
FROM ue INNER JOIN(
SELECT id
from (
SELECT #row \\:=#row+1 as rownum, id
from (
select id, #row
from ue, (select #row := -1) r
order by id) as sorted
)as ranked
where rownum %20=0) as subset
on subset.id = ue.id
where Operator like :operator
But you can use a subquery to initialize a variable like this
(select #row := -1) r

mysql query Join table doesn't work

here is my query that makes an error for Join
$query = 'SELECT
a.ks_u_id,
a.ks_keyword,
b.u_photo
FROM
'.T_KEYWORD_HISTORY.' a
WHERE a.ks_u_id in ( SELECT uf_target_id FROM '.T_USER_FOLLOW.' WHERE uf_user_id="'.$u_id.'" and uf_target_id <> "'.$u_id.'" )
JOIN '.T_USER_ACCOUNT.' b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5 ';
I get this error message.
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 'JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id ORDER BY a.ks_time DESC ' at line 8
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
ORDER BY a.ks_time DESC
LIMIT 0 , 5
It looks like I wrote wrong query for JOIN command.
Try
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5
The JOIN clause comes before the WHERE clause. Your query should be:
SELECT
a.ks_u_id, a.ks_keyword, b.u_photo
FROM T_KEYWORD_HISTORY a
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id
WHERE a.ks_u_id in ( SELECT uf_target_id
FROM T_USER_FOLLOW
WHERE uf_user_id="jake" and uf_target_id <> "jake" )
ORDER BY a.ks_time DESC
LIMIT 0 , 5

using SELECT inside INSERT query

please can anyone spot what is wrong with this query.It is giving error but i can't really see where the error is from since it is not really specified
mysql>
insert into sorting (mindlrid,maxdlrid)
values (
(select min(dlrid)
from dlr
where sid=
(select distinct(s.sendid)
from sent s
,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1))
,(select max(dlrid)
from dlr
where sid=(
select distinct(s.sendid)
from sent s,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1)
);
ERROR 1064 (42000): 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 '' at line 1
You're missing a closing parenthesis. Probably at the very end of the insert. Equivalent: there is an extra opening paren on the values clause.
INSERT INTO sorting
(mindlrid,
maxdlrid)
VALUES (SELECT Min(dlrid)
FROM dlr
WHERE sid = (SELECT DISTINCT( s.sendid )
FROM sent s,
dlr d
WHERE s.uid = d.uid
AND d.d_billed = 1
AND d.d_sent = 0
AND s.processed = 1
AND s.sendid = d.sid
ORDER BY s.sendid ASC
LIMIT 1)),
(SELECT Max(dlrid)
FROM dlr
WHERE sid = (SELECT DISTINCT( s.sendid )
FROM sent s,
dlr d
WHERE s.uid = d.uid
AND d.d_billed = 1
AND d.d_sent = 0
AND s.processed = 1
AND s.sendid = d.sid
ORDER BY s.sendid ASC
LIMIT 1));
You can use the result of a SELECT directly, no need to wrap that into a VALUES clause:
insert into sorting (mindlrid,maxdlrid)
select min(dlrid), max(dlrid)
from dlr
where sid=
(select distinct s.sendid
from sent s
,dlr d
where s.uid=d.uid
and d.d_billed=1
and d.d_sent=0
and s.processed=1
and s.sendid=d.sid
order by s.sendid asc
limit 1)
Note that DISTINCT is not a function. It's an operator that includes all columns of the SELECT list.