I have a table customers. How can I select only ids for the distinct values in the email column?
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | sergiy#gmail.com |
| 2 | bob#gmail.com |
| 3 | anre#gmail.com |
| 4 | sergiy#gmail.com |
| 5 | antony#gmail.com |
+----+------------------+
So the result will be:
[1,2,3,5]
So far, I've tried:
select distinct id, email from customers group by id;
select min(id) from Customers group by email;
Related
I'm trying to select orders, which are send at least two times with the same addressId to a customer.
This is my table structure:
Customer Table:
+------------+-----------+
| customerId | addressId |
+------------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+------------+-----------+
Relation for Addresses to Orders
+---------+-----------+
| orderId | addressId |
+---------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 4 |
+---------+-----------+
Order Table
+----+------------+-------+
| id | orderEntry | total |
+----+------------+-------+
| 1 | timestamp | 4711 |
| 2 | timestamp | 0815 |
| 3 | timestamp | 1337 |
+----+------------+-------+
Now I want a output like this:
+------------+---------+-----------+
| customerId | orderId | addressId |
+------------+---------+-----------+
| 2 | 2 | 2 |
| 2 | 3 | 2 |
| 4 | 5 | 4 |
| 4 | 6 | 4 |
+------------+---------+-----------+
I've tried to get the right result with these Query, but I think I can't count the addresses this way.
SELECT C.`customerId`, AO.`orderId`, AO`addressId`
FROM customer AS C
JOIN address_order AS AO ON AO.addressId = C.addressId
JOIN order AS O ON O.id = AO.orderId
GROUP BY AO.`orderId`
HAVING (COUNT(AO.`addressId`) > 1);
With these Query I only get a result like this:
+------------+---------+-----------+
| customerId | orderId | addressId |
+------------+---------+-----------+
| 2 | 2 | 2 |
| 4 | 5 | 4 |
+------------+---------+-----------+
I don't see the usage of order table here. However you can use the order table into the consideration if you want to make sure that the order table data and address_order should have data. You can write the query as
select
c.customerId,
ao.orderId,
ao.addressId
from customer c
join address_order ao on ao.addressId = c.addressId
join (
select addressId, count(*) as tot from address_order
group by addressId having tot = 2
)x on x.addressId = ao.addressId
If you want to make sure all the orderId from customer_order are in the order table then you can add another join at the end as
join `order` o on o.id = ao.orderId
Try this
SELECT customerId FROM customer INNER JOIN (SELECT * FROM address_order GROUP BY addressId
HAVING (COUNT(addressId) > 1)) AS t1 ON customer.addressId=t1.addressId
I am working on a project where I have to compare two tables together and sum the number of occurrences. For this example I will use name as the key that I would like to compare. I have used Union all and Count(*) but it wouldn't give me the desired output.
Table apple
+----+-------+---------+
| Id | Name | Surname |
+----+-------+---------+
| 1 | Adam | Jaxon |
| 2 | Adam | Brixton |
| 3 | Brian | Simpson |
| 4 | Adam | Steper |
| 5 | Brian | Bastion |
+----+-------+---------+
Table orange
+----+-------+---------+
| id | name | surname |
+----+-------+---------+
| 1 | Adam | Thompson|
| 2 | Brian | Coach |
| 3 | Jhon | Sinded |
+----+-------+---------+
There is one name match for Adam and one match for Brian so the desired output I would like to receive is
+-------+
| Total |
+-------+
| 3 |
| 1 |
+--------+
The query that I am using similar to the person who had answered the question however there are few changes. Unfortunately this only returns number of matches for each name
SELECT COUNT(*)
FROM
(
SELECT Name
FROM
apple
UNION ALL
SELECT
NAME
FROM
orange
) as named
GROUP BY name
+----------+
| Count(*) |
+----------+
| 2
| 3
+----------+
Try it, grouping and count name:
SELECT
id,
name,
surname
count(1) as total
FROM
(
SELECT
id,
name,
surname
FROM apple
UNION ALL
SELECT
id,
name,
surname
FROM orange
)
GROUP BY name
UPDATE
To SUM all results before your query:
SELECT sum(total)
FROM (
SELECT
id,
name,
surname
count(1) as total
FROM
(
SELECT
id,
name,
surname
FROM apple
UNION ALL
SELECT
id,
name,
surname
FROM orange
)
GROUP BY name
)
No DB server available, so haven't tested it - should do the trick, however:
SELECT COUNT(*) FROM
(
SELECT DISTINCT apple.name FROM apple
JOIN orange ON apple.name = orange.name
);
I have this code:
SELECT ID, Name, 100 AS TempColumn
FROM MyTable;
And the table is this:
| ID | Name | TempColumn|
-------------------------
| 1 | A | 100 |
-------------------------
| 2 | B | 100 |
-------------------------
| 3 | C | 100 |
-------------------------
| 1 | A | 100 |
-------------------------
| 4 | D | 100 |
-------------------------
Now I want to find the sum of the |TempColumn| where ID=1.
So it should look like this:
| ID | Name | TempColumn|
-------------------------
| 1 | A | 200 |
-------------------------
How can I query this?
You can sum a constant:
SELECT ID, Name, SUM(100) AS SumOfTempColumn
FROM MyTable
WHERE ID = 1
GROUP BY ID, Name;
Example on SQL Fiddle
Should this not be reasonably straight forward using an aggregate query?
SELECT ID,
Name,
SUM(100) AS TempColumn
FROM MyTable
GROUP BY ID, Name;
SELECT ID, Name,SUM(TempColumn) AS TempColumn
FROM Table
WHERE ID = 1
GROUP BY ID, Name
I have a table in SQL as given below: I need to select all records for a particular user_id:
mysql> select * from matusermeta limit 10;
+----------+---------+----------------------+---------------------------------+
| umeta_id | user_id | meta_key | meta_value |
+----------+---------+----------------------+---------------------------------+
| 1 | 1 | first_name | sandeep |
| 2 | 1 | last_name | kumar |
| 3 | 1 | nickname | sk4222 |
| 4 | 1 | description | |
| 5 | 1 | rich_editing | true |
| 6 | 1 | comment_shortcuts | false |
| 7 | 1 | admin_color | fresh |
| 8 | 1 | use_ssl | 0 |
| 9 | 1 | show_admin_bar_front | true |
| 10 | 1 | matcapabilities | a:1:{s:13:"administrator";b:1;} |
+----------+---------+----------------------+---------------------------------+
I am using this query "select * from wp_usermeta group by user_id"
+----------+---------+------------+------------+
| umeta_id | user_id | meta_key | meta_value |
+----------+---------+------------+------------+
| 1 | 1 | first_name | sandeep |
| 58 | 4 | first_name | test |
+----------+---------+------------+------------+
not all the records are showing for a particular id
If you want to select record of all userid in you table use:
SELECT * FROM [wp_usermeta] WHERE user_id IN (SELECT user_id FROM [wp_usermeta])
If you want to get record of specific userid use:
SELECT * FROM [wp_usermeta] WHERE user_id IN ('1','2','95','1204')
you are grouping by user_id not selecting, if you want select all records for a particular user_id, try this:
select * from wp_usermeta where user_id=your_particular_id
select * from wp_usermeta where user_id = Your_ID
You can the use the above code to fetch data for a Particular UserID
select * from wp_usermeta where user_id IN ( Your_ID1, Your_ID2, Your_ID3)
You can the use the above code to fetch data for any number of UserID
I need to find how many user brought a product (Note : I don't need total no of users only how many users brought).
Table name : buyer
+-----+----------+---------+
| id | products | uid |
|-----|----------|---------|
| 1 | soap | 2 |
| 2 | h_oil | 1 |
| 3 | soap | 3 |
| 4 | tea | 1 |
| 5 | soap | 2 |
| 6 | h_oil | 1 |
| 7 | soap | 3 |
| 8 | tea | 1 |
+-----+----------+---------+
I need a result like this table given below:
|-----|---------------|--------------|
| id | product_name | total_user |
|-----|---------------|--------------|
| 1 | soap | 2 |
| 2 | h_oil | 1 |
| 3 | tea | 1 |
|------------------------------------|
How can I do that in a MySQL query?
SELECT products product_name, COUNT(DISTINCT uid) total_user FROM purchases GROUP BY products;
You could use:
SELECT product_name, COUNT(*) AS total_user FROM buyer GROUP BY product_name;
Like this:
SELECT
COUNT(*)
FROM
buyer
GROUP BY
products
In short, you are looking for a group by statement
SELECT uid, product_name, count(id) as total_user FROM buyer GROUP BY uid
read more here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT
COUNT(id)
FROM
buyer
GROUP BY
products