Here's my current query I'm using
SELECT *
FROM accounts
WHERE source = 0
AND account_id NOT IN(SELECT receive FROM actions WHERE follow = '$account')
AND status = 0
ORDER BY RAND() LIMIT
I want to somehow do something like this
SELECT *
FROM accounts
WHERE source = 0
AND account_id NOT IN(SELECT * FROM actions WHERE follow = '$account')
AND COUNT((SELECT * FROM actions WHERE follow = '$account')) AS `total_received_follows`
AND total_received_follows < max_follows
AND status = 0
ORDER BY RAND() LIMIT
I basically need to get COUNT the amount of rows that have a follow value of '$account' from the actions table I then want to check this value has a lower value than the row max_follows from the accounts table
I understand that's probably not the correct syntax, but is their anyway I can do something like that using the query I have?
I'm a little confused, but here goes... In your second query, you're looking for accounts that don't have any corresponding rows in actions? But it also looks like you're trying to find accounts that have less than max_follows rows in actions. Am I right? You realise this is contradictory?
Regardless, this might be a place to start:
SELECT acc.*, COUNT(act.id) act_count FROM accounts acc
LEFT JOIN action act ON act.follow = acc.id
WHERE ...
GROUP BY acc.id
HAVING act_count < acc.max_follows
Related
I have a table name "chat_details" I wanna access only data with green underline according to time, I use the following query
//suppose $user_id = 1;
"SELECT *
FROM chat_details WHERE from_user_id='$user_id' OR to_user_id='$user_id' ORDER BY time DESC"
It fetch all the rows because all rows contain user_id = 1 in one of the column, but i need only green underline rows as compare to red one because green one are latest according to time(column), how can i fetch only these green underlines?
This is one posible query, that should solve your question.
SELECT *
FROM chat_details c
WHERE (c.from_user_id='$user_id' OR c.to_user_id='$user_id')
AND NOT EXISTS (
SELECT 1 FROM chat_details d
WHERE d.from_user_id = c.from_user_id
AND d.to_user_id = c.to_user_id
AND d.time > c.time)
ORDER BY c.time DESC"
Actually I could not test, hope I haven't made a mistake.
The query selects all data as in your query, but only thoose which haven't a newer chat between the two users.
For the EXISTS keyword see http://www.mysqltutorial.org/mysql-exists/
Please can someone talk me through a query that I have inherited through a website I am developing.
The query is returning a random group of 5 products based on the category number 56. There is an issue with the query because it is not restricting the selection based on the product on web and product archive conditions.
AND p.product_OnWeb = 1
AND p.product_Archive = 0
The above lines in the query aren't being adhered to. Instead the query is including all products even when they are marked as p.product_Archive=1 (Archived) and p.product_OnWeb = 0 (Not Online)
If someone could point out where I need to make a change I'd be grateful.
The query in full is:-
SELECT c.prdt_cat_rel_Product_ID,
ROUND(RAND() * x.m_id) 'rand_ind'
FROM tbl_prdtcat_rel c,
tbl_products p,
(SELECT MAX(t.prdt_cat_rel_Cat_ID) 'm_id'
FROM tbl_prdtcat_rel t) x
WHERE c.prdt_cat_rel_Cat_ID = 56
AND p.product_OnWeb = 1
AND p.product_Archive = 0
ORDER BY rand_ind
LIMIT 3
Thankyou
First, to make it easier, convert the query to the newer join syntax
SELECT c.prdt_cat_rel_Product_ID, ROUND(RAND() * x.m_id) 'rand_ind'
FROM tbl_prdtcat_rel c,
JOIN tbl_products p ON p.??? = c.???
JOIN (
SELECT MAX(t.prdt_cat_rel_Cat_ID) 'm_id' FROM tbl_prdtcat_rel t
) x ON 1=1
WHERE c.prdt_cat_rel_Cat_ID = 56
AND p.product_OnWeb = 1
AND p.product_Archive = 0
ORDER BY rand_ind
LIMIT 3
You can see that the query doesn't know how to select match records from P based on C, so it grabs them all.. You need to specify how to match tbl_prdtcat records with tbl_product records (replace the ??? in the above with the appropriate fields)
I am guessing each product in p has some sort of field indicating which category it belong to, use this field to match and your query should work...
I have two tables:
client : (backoffice_users_id, idfile, validation_file)
transmission : (backoffice_users_id, idfile, nbr_files)
I want to show the client who has the minimum number of files and with validation_file=1
I did something like this, it gives me the minimum number but with validation_file=0 not validation_file=1, can anyone help me please?
SELECT `transmission`.*
FROM `transmission`
INNER JOIN `client`
ON client.backoffice_users_id=transmission.backoffice_users_id
WHERE (transmission.idfile='2')
AND (client.validation_file<>0)
AND (transmission.nbr_files=(
SELECT min(nbr_files)
FROM transmission
WHERE transmission.idfile='2'))
GROUP BY `client`.`backoffice_users_id`
LIMIT 1
I don't know what your transmission.idfile='2' means so only answering the 'minimum number of files and with validation_file=1'-part and with the minimume number of files within one single transmission (not summed over multiple transmissions):
SELECT * FROM `transmission`
INNER JOIN `client` ON
`client`.`backoffice_users_id` = `transmission`.`backoffice_users_id` AND
`client`.`validation_file` = 1
ORDER BY `transmission`.`nbr_files` ASC
LIMIT 1
Is there a way to convert the selected content of 0 or 1 to no or yes and search from bottom of table up?
UPDATE #__comprofiler
SET cb_trainingpass = ( SELECT c_passed
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id)
WHERE EXISTS
( SELECT c_passed
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id);
As users take their test, they get a result of 0 = not-passed, or 1 = passed. I am sending this to the field cb_trainingpass and would like it to be yes (for passed) or no instead. Also, users take the test multiple times and their newest results is what I am trying to pull, unfortunately this query pulls the one at the top or the first result, never finding the newest.
try something like
UPDATE #__comprofiler
SET cb_trainingpass = ( SELECT if(c_passed=1,'yes','no')
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id
order by #__quiz_r_student_quiz.YOURDATEFIELD desc)
WHERE EXISTS
( SELECT c_passed
FROM #__quiz_r_student_quiz
WHERE #__quiz_r_student_quiz.c_student_id = #__comprofiler.user_id);
i have two tables i am trying to get information from.
login table - which has the list of employees
projects table - which has the projects
in short, i am trying to write a query that will select the copywriters and perform a subquery on each that will return a field dubbed 'open_projects'. This, i can get to work with the below sql:
select web_login_id,
(select count(project_web_id) from project
where copywriter = web_login_id
and (`status` = 'open' or `status` = 'qual')) as open_projects from login
where roles like '%copywriter%'
and tierLevel like '%c1%'
order by open_projects asc
This returns something like:
1982983 3
1982690 22
2987398 5
The problem with this is that sometimes 5 or 6 of the projects will belong to the same client and are not actually being worked on as they are dealt with in a queue-ish fashion.
My question is how to modify the above sql so that the subquery will GROUP subset based on the client_login_id field.
This sql gives me an error of : subquery returns more than 1 row
select web_login_id,
(select count(project_web_id) from project
where copywriter = web_login_id
and (`status` = 'open' or `status` = 'qual') group by client_login_id) as open_projects from login
where roles like '%copywriter%'
and tierLevel like '%c1%'
order by open_projects asc
You need to rephrase this as an explicit join. I think the following does the trick:
select web_login_id, cw.open_projects
from login l left outer join
(select copywriter, count(project_web_id) as open_projects
from project
where `status` in ('open', 'qual')
group by copywriter
) cw
on l.web_login_id = cw.copywriter
where l.roles like '%copywriter%' and l.tierLevel like '%c1%'
order by open_projects asc
I'm not sure what the "group by client_login_id" is doing. It doesn't seem necessary.
Once you've done this, you can return as many columns as you like from the subquery.