I have a a table with following values:
id employee_id project_id
1 1 1
2 2 1
3 3 1
4 4 1
How can i list by project_id? using mysql queries?
project_id employee_id employee_id employee_id employee_id
1 1 2 3 4
Try below query-
SELECT project_id ,SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',1) AS employee_id1,SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',2),',',-1) AS employee_id2, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',3),',',-1) AS employee_id3, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',4),',',-1) AS employee_id4 FROM my_table GROUP BY project_id;
SELECT project_id, GROUP_CONCAT(employee_id) FROM Table GROUP BY project_id;
It's can be consider ..??
Related
I am using mariadb and I have a table called links:
id | product_id | last_change
------------------------------
1 1 xxx
2 2 xxx
3 5 xxx
4 5 xxx
I want to find every object (3, 4 in this example) that occures more than once. Following this answer I tried:
SELECT product_id, COUNT(*) from links HAVING COUNT(*) > 1
But this results in the (adapted to this example) first row being shown and the total number of product_id occurrences:
product_id | COUNT(*)
---------------------
1 4
I wanted to achieve a list of all items occuring more than once:
id | product_id | last_change
------------------------------
3 5 xxx
4 5 xxx
An aggregation function without GROUP BY always results in only one row result as it aggregates all rows
So use a GROUP BY
SELECT product_id, COUNT(*) from links GROUP BY product_id HAVING COUNT(*) > 1
To see all entry with the count of the product_id , you can do following
SELECT l1.product_id , last_change , Count_
FROM links l1
JOIN (SELECT product_id, COUNT(*) as Count_ from links GROUP BY product_id HAVING COUNT(*) > 1) l2
ON l1.product_id = l2.product_id
Try below statement
select id, product_id, count(product_id)
from links
group by (product_id)
having count(product_id)> 1;
I would like to count(*) how much customers have created a post or made a comment. If the same customer has made several posts and comments, it should count only once.
Customer Table:
ID Name ...
1 Jonh
2 Mark
3 King
4 Doe
Post Table:
ID USER_ID...
1 1
2 1
3 3
4 1
Comment Table:
ID USER_ID...
1 1
2 3
3 3
4 4
It should return count(*) = 3
(user_id: 1, 3 and 4).
Try this one. It worked for me and returns what you're looking for:
SELECT COUNT( USER_ID ) AS TOTAL
FROM (
SELECT USER_ID
FROM POSTS
UNION
SELECT USER_ID
FROM COMMENTS
)X
I used POSTS and COMMENTS as table names bc I was unsure what your exact table names are, so make sure to change these in your query.
This should work:
SELECT COUNT(DISTINCT USER_ID) FROM (
SELECT USER_ID FROM POST_TABLE
UNION
SELECT USER_ID FROM COMMENT_TABLE
)
I need to list all the duplicate IDs with the number of occurrence of each ID in a single MYSQL query.
ID
____
1
1
2
3
4
4
4
5
5
6
7
Output must be:
ID | Occurrence
_______________
1 | 2
4 | 3
5 | 2
Just use a simple GROUP BY query:
SELECT ID, COUNT(*) AS Occcurrence
FROM yourTable
GROUP BY ID
HAVING COUNT(*) > 1
This can be done simply by using Group By clause and Count() function
select
Id, count(id) as Occurance
from
tableName
group by id
having Occurance > 1;
use mysql GROUP BY
select ID,count(*) from table_name group by ID having count(*) > 1
Sample data:
ProductID PackingID
------- ---------
1 2
1 2
3 2
3 2
1 1
2 1
3 2
I have the above sample data. What i want is to select the unique (not distinct) rows of the combination productID and packingID. In the above example the only matching results are
ProductID PackingID
------- ---------
1 1
2 1
These rows are the only unique combinations of ProductID and PackingID together. I do not want Distinct results because it will give me one of all the other combinations.
SELECT PRODUCTID,PACKINGID FROM DTEMP
GROUP BY PRODUCTID,PACKINGID
HAVING COUNT(PRODUCTID)=1
ORDER BY 1;
You can try this one this is how i do in oracle ... to get the unique rows without using distinct.
SELECT ProductID, PackingID
FROM yourtable
GROUP BY ProductID, PackingID
HAVING COUNT(*) = 1
your table should be like:
uniqueID ProductID PackingID
1 x y
2 x y
3 z x
Query:
SELECT uniqueID,ProductID,PackingID
FROM yourtable
WHERE uniqueID IN
(
SELECT MIN(uniqueID)
FROM yourtable
GROUP BY ProductID,PackingID
)
I would like to each ID have 2 Custom_Column values.
How can I do that?
Example Table:
ID
1
2
3
Example Query:
SELECT id, (id*2) as Custom_Value, (id*4) as Custom_Value FROM numbers
Response:
ID Custom_Value
1 2
1 4
2 4
2 8
3 6
3 12
use UNION
SELECT ID, (ID*2) as CustomVal FROM tableNAme
UNION ALL
SELECT ID, (ID*4) as CustomVal FROM tableNAme
ORDER BY ID
SQLFiddle Demo