I'm trying to make an event in phpmyadmin that will get the number of rows in a number of tables and then insert the results into another database. The only issue is I can't seem to be able to use mysql to count the rows and then also put them into the table. I've also tried to set mysql variables with the COUNT(). Here is the current code that I have:
INSERT INTO user_count (users,taps,statues,questions,friendships,expressions)
SELECT COUNT(*) from `users`,COUNT(*) from `taps`,COUNT(*) from `status`,
COUNT(*) from `questions`,COUNT(*) from `friends`,COUNT(*) from `expressions`;
You are almost done. Following query would be worked for you.
INSERT INTO user_count (users, taps, statues, questions, friendships, expressions)
SELECT
(SELECT COUNT(*) from `users`)
(SELECT COUNT(*) from `taps`)
(SELECT COUNT(*) from `statues`)
(SELECT COUNT(*) from `questions`)
(SELECT COUNT(*) from `friendships`)
(SELECT COUNT(*) from `expressions`)
;
Related
Im trying to select * from all duplicate rows in users, where a duplicate is defined as two users sharing the same first_name and last_name. (I need to process the other columns that might differ)
Im using MySQL 8.0.28.
My first try was to literally translate my requirement:
select * from `users` AS u1 where exists (select 1 from `users` AS u2 WHERE `u2`.`first_name` = `u1`.`first_name` AND `u2`.`last_name` = `u1`.`last_name` AND `u2`.`id` != `u1`.`id`)
Which, obviously, has a horrendous execution time.
My current query is
SELECT * from users where Concat(first_name," ",last_name) IN (select Concat(first_name," ",last_name) from `users` GROUP BY first_name, last_name HAVING COUNT(*)>1)
which is vastly more efficient, but still takes more than 100ms for 8000 records. I suppose a solution that doesn't use concat could benefit from indicies and would not need to calculate the result for each row.
Also, I couldn't get group by to work because I need so select all columns of all rows that are duplicates, not just the distinct first_name's and last_name's. Also because I don't want to disable ONLY_FULL_GROUP_BY (not sure if disabling that would help anyway).
Is there a more efficient, proper way to select these duplicate rows?
I would just use an aggregation approach here:
SELECT *
FROM users
WHERE (first_name, last_name) IN (
SELECT first_name, last_name
FROM users
GROUP BY 1, 2
HAVING COUNT(*) > 1
);
On MySQL 8+, we can also use COUNT() as an analytic function here:
WITH cte AS (
SELECT *, COUNT(*) OVER (PARTITION BY first_name, last_name) AS cnt
FROM users
)
SELECT *
FROM cte
WHERE cnt > 1;
I want to insert three values or three rows for each user I have. I know I can use the following query
INSERT INTO subscriptions(id_account, subscription)
SELECT id_account, 'subscriptionVAlue' FROM unnest ('{123,456,789}'::int[]) id;
to insert one row for each id in the list.
but I want is something like this
INSERT INTO subscriptions (account_id, subscription, i18nkey)
VALUES (id, 'subscription1', 'translation1')
VALUES (id, 'subscription2', 'translation2')
VALUES (id, 'subscription3', 'translation3')
where id in (123,1234,410,4512);
This is such bad query I know, but I want similar kind of behavior. Is that possible?
You can use cross join:
INSERT INTO subscriptions (account_id, subscription, i18nkey)
SELECT a.account_id, subscription, i18nkey
FROM (SELECT 'subscription1' as subscription, 'translation1' as i18nkey UNION ALL
SELECT 'subscription2', 'translation2' UNION ALL
SELECT 'subscription3', 'translation3'
) x CROSS JOIN
(SELECT 123 as account_id UNION ALL SELECT 1234 UNION ALL
SELECT 410 UNION ALL SELECT 4512
) a;
unnest() is not MySQL syntax. You can also do something similar with unnest(). The above should work in almost any database, although you might need a FROM clause (such as FROM dual) for the subqueries.
I am trying to combine two SQL statements into one, but am running into GROUP BY errors for full_group_only, which I understand why, but not how to solve.
In the first query, I am simply getting the number of actions per item, and in the second query, I am getting the latest action.
Assume a simple setup as:
actions (id, item_id, description)
items (id, name)
With the two queries
SELECT item_id, COUNT(*) AS actions_number FROM actions GROUP BY item_id
SELECT * FROM actions WHERE id in (SELECT max(id) FROM actions GROUP BY item_id)
How do I easily combine these two statements into one?
Is this what you want?
SELECT
a.*, b.actions_number
FROM
actions a
INNER JOIN
(SELECT
MAX(id) id, COUNT(*) actions_number
FROM
actions
GROUP BY item_id) b ON a.id = b.id;
i am using MySql workbench 5.7 to run this.
i am trying to get the result of this query:
SELECT COUNT(Users) FROM UserList.custumers;
and this query:
SELECT Users FROM UserList.custumers;
at the same table, meaning i want a list of users in one column and the amount of total users in the other column.
when i tries this:
SELECT Users , COUNT(Users) FROM UserList.custumers;
i get a single row with the right count but only the first user in my list....
You can either use a cross join since you know the count query will result in one row... whose value you want repeated on every row.
SELECt users, userCount
FROM userlist.custumers
CROSS JOIN (Select count(*) UserCount from userlist.custumers)
Or you can run a count in the select.... I prefer the first as the count only has to be done once.
SELECT users, (SELECT count(*) cnt FROM userlist.custumers) as userCount
FROM userlist.custumers
Or in a environment supporting window functions (not mySQL) you could count(*) over (partition by 1) as userCount
The reason you're getting one row is due to mySQL's extension of the GROUP BY which will pick a single value from non-aggregated columns to display when you use aggregation without a group by clause. If you add a group by to your select, you will not get the count of all users. Thus the need for the inline select or the cross join.
Consider: -- 1 record not all users
SELECT Users , COUNT(Users) FROM UserList.custumers;
vs --all users wrong count
SELECT Users , COUNT(Users) FROM UserList.custumers group by users;
vs -- what I believe you're after
SELECT Users, x.usercount FROM UserList.custumers
CROSS JOIN (Select count(*) UserCount from userlist.custumers) x
Use a subquery in SELECT.
Select Users,
(SELECT COUNT(Users) FROM UserList.custumers) as total
FROM UserList.custumers;
How to do get distinct count(*) in MySQL.
for example, in table1 i have 10 million record, there are duplicate records in it.
I want to find out distinct count(*) from the table.
I know, I can do
select distinct * from table1
but, i don't want to fetch 10 million records, not even want to insert distinct records in other table like,
create table table2 select distinct * from table1
So, please help me with any other option.
Help from anyone welcome
SELECT COUNT(DISTINCT field) FROM table
or
SELECT COUNT(*) FROM table GROUP BY field;
(btw - this has been answered quite a few times elsewhere on this site)
Try using a subquery:
SELECT COUNT(*) FROM (SELECT DISTINCT * FROM table1) T1
Maybe like:
SELECT SUM(cnt) FROM ( SELECT COUNT(*) as cnt FROM tab GROUP BY some_value )