Here's the simple query:
SELECT status FROM tbl_pedidos_produtos WHERE status = 4;
This, obviously, brings me only the entries whose status equals to 4, but in this manner I can't test if ALL entries have status 4. How can I do something like this?
SELECT status FROM tbl_pedidos_produtos WHERE status OF ALL = 4;
Simply, just get COUNT(*) of the rows those doesn't have status = 4
SELECT COUNT(*) FROM tbl_pedidos_produtos WHERE status != 4;
If it's greater than 0, that means you have at least one row which has status != 4.
You can do this using aggregation. Here is one way:
select (max(status) = 4 and min(status) = 4 and status is not null) as AllSameFlag
from tbl_pedidos_produtos;
Another way that might be a bit less obvious:
select (count(*) = 0) as AllSameFlag
from tbl_pedidos_produtos
where status <> 4 or status is null
If you don't have null values in there, you can do that in a single query by selecting all distinct status values, counting them, then make sure that adds up to 1. Like this:
SELECT (SELECT COUNT(DISTINCT `status`) from `tbl_pedidos_produtos`) = 1;
If you do have null values (i.e., a product without a status value for some reason), then you'll need to filter them out first.
Related
I just want to perform multiple table operations in single query. The query is:
select name,sno , id as ids ,(select sum(amount) from client_credits
where user_id = ids) As total from clients where sno = '4' and total > '0'
This query is not working when I am trying use ** total > 0 **. Is there any other possibility ways? Please help me.
total is an alias. Aliases are not resolved when the WHERE clause is reached.
Try: where sno = 4 having total > 0
Alternatively, and more efficiently:
SELECT `c`.`name`, `c`.`sno`, `c`.`id`, SUM(`ccr`.`amount`) AS `total`
FROM `clients` AS `c`
JOIN `client_credits` AS `ccr` ON `c`.`id`=`ccr`.`user_id`
WHERE `c`.`sno` = 4
GROUP BY `c`.`id`
Notice how the total > 0 part is gone? That's because if there are no rows to join, then nothing will be joined and the rows are removed from the result ;)
hi i need to get the plistid based on a given combination of avid. For example i submitted a combination of 1 and 4 avid, it should return a plistid of 1. Another example is when i submitted a combination of 2 and 5 avid it should return a plistid of 5. And if i submitted 1 and 3 it should return nothing since they are of the same attributeid.
How can i generate it in mysql
here is my table
NOTE: avid can be a combination of 1,2,3 or 4 numbers depending on the number of avid's submitted.
First, find all the matches of avid by plistid and then check that they have different attributeid. You can do this with aggregation, as a variation of a set-within-sets query:
select plistid
from t
group by plistid
having sum(avid = 1) > 0 and
sum(avid = 4) > 0 and
count(distinct case when avid in (1, 4) then attributeid end) = 2
The first two conditions in the having clause are saying what avids you want. They are counting the number of times that avid appears with one of the values, each returns true only when at least one value is found. The last is saying that you need distinct attribute ids on the rows.
Try:
select plistid
from t
where avid in (1,4)
group by plistid
having count(distinct avid) =2
What about this?
SELECT t1.plistid
FROM t t1
JOIN t t2
ON t1.attributeid != t2.attributteid
AND t1.plistid = t2.plistid
WHERE
t1.avid = 1
AND t2.avid = 4
try this one:-
select a.plistid
from your_table a, your_table b
where a.plistid = b.plistid
and a.avid = <first param>
and b.avid = <second param>
and a.attributeid <> b.attributeid;
I have customers on my DB with too many tickets, and these tickets have too many statuses. I want a query that could pull the cutomer's ticket number, only if the ticket with status!='Closed'. Otherwise it should return null.
Your fast response is highly appreciated.
Edit: Here is my query:
SELECT CASE WHEN custom_field_3 != 'Closed' THEN item_id ELSE NULL END AS ticketID
FROM aims_items
WHERE custom_field_22 =221226
It works now, and here is my solution:
SELECT
CASE WHEN
(
SELECT COUNT(*)
FROM aims_items
WHERE custom_field_3 != 'Closed'
AND custom_field_22 =221226
)>0
THEN
item_id
ELSE
NULL
END
AS ticketID
FROM aims_items
WHERE
custom_field_3 != 'Closed'
AND
custom_field_22 =221226
Thanks for your cooperation.
Try this query:
SELECT IF(custom_field_3!='Closed',item_id,'') as ticketID FROM `aims_items`
Remember, i have not written the exact fields that you have in your table. This is just example to show you. Refer to below link for detailed informationhttp://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
Without table structure it is not possible to evaluate what the issue is. From your description a simple query like
SELECT item_id from aim_items WHERE custom_field_3 != 'Closed' AND custom_field_22 = 221226
Would suffice. You can then count the number of rows returned to determine result.
If, however, you require null to be returned as a result, try the following :
SELECT (SELECT item_id from aim_items WHERE custom_field_3 != 'Closed' AND custom_field_22 = 221226) AS ticketID
The above encapsulates the basic select into a subquery, to provide a null result.
Well I have this mysql table with numbers in one column and a confirmation boolean of 0 or 1 and I have about 1,000 rows so it's not something I can do manually but anyways...
I want to sort the row by highest value and grab the names of the first 5 people and put those 5 people in another table on a column and then set them to confirmed and continue until there's no one left in the table that isn't confirmed...
ex:
Name:Rank:Confirm
Bob:5000:0
James:34:0
Josh:59:1
Alex:48:0
Romney:500:0
Rolf:24:0
Hat:51:0
so when you run the code it will do the following:
Squad:Name1:Name2:Name3:Name4:Name5
1:Bob:Romney:Hat:Alex:James
(as you can see Josh was excluded and Rolf was too low)
And since Rolf is alone and there are no one else left, he wont be put into a team and will be left unconfirmed...
I'm not really pro at mysql so I was stumped on this and at most was capable of organizing the whole thing by rank and that's it ._.
edit:
The terrible attempt I had at this:
<?php
$parse = mysql_query("SELECT MAX(rank) AS rank FROM users AND confirm='0'");
mysql_query("Insert into squad (nameone)values($parse)");
mysql_query("Update squad set confirm = '1' where name = $parse");
?>
Assuming confirm will have only either 1 or 0.
CREATE TABLE table2 (id INT PRIMARY KEY AUTO_INCREMENT, name varchar(255));
CREATE PROCEDURE rank()
BEGIN
DECLARE count INT DEFAULT 1;
WHILE count > 0 DO
UPDATE table1 SET Confirm=2 WHERE Confirm=0 ORDER BY Rank DESC LIMIT 5;
INSERT INTO table2 (SELECT GROUP_CONCAT(Name) FROM table1 WHERE Confirm=2);
UPDATE table1 SET Confirm=1 WHERE Confirm=2;
SELECT count(*) FROM table1 WHERE Confirm=0;
END WHILE;
END;
Call the procedure rank() when ever you want
CALL rank();
I have a table called replies and it has a column named topic_id.
What I want to do is count how many rows have the topic_id 1.
I tried
SELECT COUNT(*)
FROM [***].[replies]
WHERE [***].[topic_id] = '1'
But in this case I don't know if this is a valid query. If it is, how can I pull it back into a variable called $num_rows in my php code?
Note: * is actually a prefix I covered.
Something like this should work:
--declare the variable
DECLARE #num_rows INT
--get the total number of rows
SELECT #num_rows = COUNT(*) FROM replies WHERE topic_id = 1
--get a distinct count of rows (if for some reason you need it)
SELECT #num_rows = COUNT(DISTINCT reply_id) FROM replies WHERE topic_id = 1
--return the result set
SELECT #num_rows AS num_rows
If you just want to return a column called num_rows though, this would be a much more efficient approach:
SELECT COUNT(*) AS num_rows FROM replies WHERE topic_id = 1