The table structure is like this:
actions: int(10)
unlock: tinyint(1)
user_id: int(20)
name: varchar(50)
I have such query:
SELECT SUM(actions) AS "sum_actions", SUM(unlock) AS "sum_unlock", user_id, name
FROM mytable AS `Results` WHERE user_id != 0 GROUP BY user_id
ORDER BY sum_actions DESC LIMIT 0,300
This gives #1064 - You have an error in your SQL syntax error.
When I remove SUM(unlock) AS "sum_unlock" then query works. So I thought that it is not possible to summing TINYINTs. So I changed to COUNT(unlock) as "count_unlock" but this didn't help. I don't want to change "unlock" table to INT because it only has boolean values. How can I count the unlock table with summing for each user_id ?
unlock is a reserved word. Try this:
SELECT SUM(actions) AS "sum_actions", SUM(`unlock`) AS "sum_unlock", user_id, name
FROM mytable AS `Results`
WHERE user_id != 0
GROUP BY user_id
ORDER BY sum_actions DESC
LIMIT 0,300
Here is a list of reserved words.
You can try SUM(CAST(unlock AS INT)) to count as if the column was an INT column without actually changing it to be an INT column:
SELECT
SUM(actions) AS "sum_actions",
SUM(CAST(unlock AS INT)) AS "sum_unlock",
user_id,
name
FROM
mytable AS `Results`
WHERE
user_id != 0
GROUP BY
user_id,
name
ORDER BY
sum_actions DESC
LIMIT 0,300
Related
goal id total occurance of goal id total occurance when status is 1
1 5 3
This is schema of the table
CREATE TABLE `goal_objectives` (
`objective_id` int(11) NOT NULL ,
`objective_name` varchar(255) NOT NULL,
`objective_description` tinytext NOT NULL,
`goal_id` int(11) NOT NULL,
`objective_status` tinyint(4) NOT NULL
);
select goal_id, count(objective_status)as objective_done
from goal_objectives
where objective_status='1' group by goal_id;
select goal_id,count(goal_id) as total_current_goals
from goal_objectives
group by goal_id
order by goal_id DESC ;
I just want to show the combine result of these two queries.
Individually it returns required result but when i try to merge them is does not work
See the output in the link below:
https://i.imgur.com/6Rnac89.png
Use conditional aggregation:
select goal_id, count(*) as total_current_goals,
sum( objective_status = 1 ) as objective_done
from goal_objectives
group by goal_id
order by goal_id desc ;
Note that objective_status is a number. The comparison value should be a number, not a string.
if I execute this query
SELECT user_ids FROM table2 WHERE `id` = 100
I get a comma separated list : 12,45,268 , user_ids is a varchar(255) field
if I execute this query
SELECT user_id FROM table1 WHERE group_id IN(12,45,268)
I get what I want
but I need
SELECT user_id FROM table1 WHERE group_id IN
(SELECT user_ids FROM table2 WHERE `id` = 100 );
but I get only user_id(s) from id = 12
maybe a problem of conversion between varchar(255) and comma separated integer id ?
thanks for helping
Use MySQL INSTR() Function. Try this instead:
SELECT user_id FROM table1 WHERE
INSTR((SELECT user_ids FROM table2 WHERE `id` = 100 LIMIT 1),group_id)>0;
How can I query to find the most recent message in a table, when the values in the column sendTime are all null?
I have tried.
SELECT `from`
,MAX(column) AS most_recent_message
FROM table
GROUP BY `from`
if you have an auto increment primary key, then you can find the maximum primary key belongs to which column.
SELECT * FROM your_table WHERE id = (SELECT MAX(id) FROM your_table);
I would like to get the number of rows returned from a mysql query that uses group by.
table
CREATE TABLE IF NOT EXISTS TestRunSteps (
`idTestRunSteps` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idUsersExecBy` VARCHAR(10) NULL ,
`LastExecUserIPV4` INT UNSIGNED NULL ,
PRIMARY KEY (`idTestRunSteps`);
SELECT count(*)
from proj1_db.TestRunSteps
group by idUsersExecBy,LastExecUserIPV4
returns
3,000002,3232236222
1,000003,3232236222
5,000004,3232236222
What I would like to have is a simple 3 - for 3 rows. Please tell me how
The number of groups is the number of distinct combinations of the columns grouped by. The query that returns that number is:
select count(distinct idUsersExecBy, LastExecUserIPV4)
from proj1_db.TestRunSteps
select count(*)
from
(SELECT count(*)
from proj1_db.TestRunSteps
group by idUsersExecBy,LastExecUserIPV4) as temp
Note: This might be a strange question.
I have a table containing first name and last name, which schema as follow (table name: random_names):
id INT PRIMARY, AUTO INCREMENT
first_name VARCHAR(100) NOT NULL
last_name VARCHAR(100) NOT NULL
I would like to use a query to fetch a random value from first_name and last_name. Currently I use 2 queries to fetch the value:
SELECT first_name FROM random_names ORDER BY rand()
SELECT last_name FROM random_names ORDER BY rand()
But I wish I can output a list of random results in 1 result output. What did I miss ?
select
(select first_name from random_names order by rand() limit 1) as random_first_name,
(select last_name from random_names order by rand() limit 1) as random_last_name;
though for tables of any size it is much faster if you programmatically determine the number of entries and pick a random offset for each column:
select
(select first_name from random_names order by rand() limit $first_name_offset,1) as random_first_name,
(select last_name from random_names order by rand() limit $last_name_offset,1) as random_last_name;
where the offsets are a random number from 0 to one less than the result of select count(*) from random_names.
Followup question:
but how about list out result count equal to the number of values in original table? (just like shuffle the data in the table)
I'd do that like this:
create temporary table rand_last (id int(11) primary key auto_increment, last_name text) select last_name from random_names order by rand();
create temporary table rand_first (id int(11) primary key auto_increment, first_name text) select first_name from random_names order by rand();
select first_name, last_name from rand_first inner join rand_last using (id);
or possibly like this (assuming random_names has an 'id' primary key):
create temporary table rand_one (id int(11) primary key auto_increment, random_names_id int(11)) select id random_names_id from random_names order by rand();
create temporary table rand_two (id int(11) primary key auto_increment, random_names_id int(11)) select id random_names_id from random_names order by rand();
select rand_first.first_name, rand_last.last_name from rand_one inner join rand_two using (id) inner join random_names rand_first on rand_one.random_names_id=rand_first.id inner join random_names rand_last on rand_two.random_names_id=rand_last.id;
You can get all possible pairs of first_name and last_name in random order by following query:
select *
from (select first_name from random_names) a,
(select last_name from random_names) b
order by rand();
You can also do like this-
SELECT first_name, last_name FROM random_names ORDER BY rand()
OR
SELECT concat(first_name," ",last_name) fullname FROM random_names ORDER BY rand()