I have mysql database with two tables.
First (information)
+---------+------+----------+
| species | sex | user |
+---------+------+----------+
| bird | NULL | 1 |
| bird | f | 1 |
| cat | f | 1 |
| cat | m | 1 |
| dog | f | 1 |
| dog | m | 2 |
| hamster | f | 2 |
| snake | m | 1 |
+---------+------+----------+
Second (users)
+--------+-----+
| user | id |
+--------+-----+
| amy | 1 |
| dav | 2 |
| mot | 3 |
| mmm | 4 |
| aaw | 5 |
| dsa | 6 |
+--------+-----+
I want to count and show values from table "information" for each user row on table "users"
Like this
+---------+------+----------+
| user | id | count |
+---------+------+----------+
| amy | 1 | 6 |
| dav | 2 | 2 |
| mot | 3 | 0 |
| mmm | 4 | 0 |
| aaw | 5 | 0 |
| dsa | 6 | 0 |
+---------+------+----------+
How can I do this query?
select users.user, users.id, count (species.name)
from users left join species
on users.id = species.user
group by users.user, users.id
order by count (species.name) desc
Isn't it something like:
select u.user, u.id, count(i.user)
from user u
inner join information i on i.user = u.id
group by u.user, u.id
Related
How to join these to in one sql statement?
I have these 2 simple tables.
3_Referee_Matches
+------------------------------------+
| ID | CountryCode | RefereeUrlCode |
| 1 | eng | mike-jean |
| 2 | eng | mike-jean |
| 3 | eng | mike-jean |
| 4 | eng | mike-jean |
| 5 | spa | hulo-pape |
| 6 | ita | enri-tolsi |
| 7 | ita | enra-ean |
| 8 | ita | enra-ean |
+------------------------------------+
3_Players
+----------------------------------------------------+
| ID | MatchID | Name | PlayerUrlCode | Yellow |
| 1 | 1 | Mike Bell | mike-bell | 1 |
| 2 | 2 | Mike Bell | mike-bell | 1 |
| 3 | 3 | Thoms Tim | thoms-tim | 1 |
| 4 | 4 | Jean Claod | jean-claod | 0 |
| 5 | 33 | Thoms Tim | thoms-tim | 1 |
| 6 | 44 | Fis Most | fis-most | 0 |
| 7 | 54 | Geni Toens | geni-toens | 1 |
| 8 | 67 | Geni Toens | geni-toens | 1 |
+----------------------------------------------------+
Today i use these 2 select. But need help to combine them into one.
select 1:
SELECT rm.*, p.PlayerUrlCode AS VALUEtoBEusedAGAIN, COUNT(p.ID) AS YellowCounter
FROM 3_Referee_Matches rm
JOIN 3_Players p ON rm.ID = p.MatchID
WHERE rm.CountryCode = 'eng' AND rm.RefereeUrlCode = 'mike-jean'
AND p.Yellow>0
GROUP BY p.Name
select 2:
SELECT COUNT(rm.ID) AS Counter
FROM 3_Referee_Matches rm
JOIN 3_Players p ON rm.ID = p.MatchID
WHERE rm.RefereeUrlCode='mike-jean'
AND p.PlayerUrlCode='VALUEtoBEusedAGAIN'
Result should be like this:
+--------------------------------------+
| Name | YellowCounter | Counter |
| Mike Bell | 2 | 2 |
| Jean Claod | 1 | 1 |
+--------------------------------------+
Showing that Mike Bell Got 2 yellow cards in 2 matches.
Your first query is nearly there. First, remove the extraneous columns from the select clause. count the number of "players" (really player information for each match) and sum their yellow cards.
select
p.name,
sum(yellow) as YellowCounter,
count(p.id) as Counter
from 3_Referee_Matches rm
join 3_Players p on rm.ID = p.MatchID
where rm.CountryCode = 'eng'
and rm.RefereeUrlCode = 'mike-jean'
group by p.name;
+------------+---------------+---------+
| name | YellowCounter | Counter |
+------------+---------------+---------+
| Mike Bell | 2 | 2 |
| Thoms Tim | 1 | 1 |
| Jean Claod | 0 | 1 |
+------------+---------------+---------+
I assume the example has Thoms and Jean reversed.
I have a wordpress database with default users table and two custom tables as below
1. wp_users
| id | display_name |
|----|--------------|
| 1 | Ibbs |
| 2 | Nina |
| 3 | rakib |
2. wp_invite
| post_id | user_id | status |
|---------|---------|----------|
| 3342 | 1 | accepted |
| 3342 | 2 | accepted |
| 3342 | 3 | accepted |
3. wp_rating
| id | reviwer | reviewed | post | know | skill | time | comm |
|----|---------|----------|------|------|-------|------|------|
| 2 | 3 | 1 | 3342 | b | b | b | b |
| 5 | 1 | 2 | 2122 | a | c | d | a |
| 7 | 2 | 3 | 3342 | d | a | b | c |
i want to select * from wp_invite where status = accepted, display_name from wp_users and then want to exclude rows from the result where all of these three conditions meet
1. wp_invite.user_id is not equal to wp_rating.reviewer and
2. wp_invite.user_id is not equal to wp_rating.reviewed and
3. wp_invite.post_id is not equal to wp_rating.post.
My desired output for post = 3342 and reviewer = 3
| user_id | display_name |
|---------|--------------|
| 2 | Nina |
| 3 | rakib |
My desired output for post = 3342 and reviewer = 2
| user_id | display_name |
|---------|--------------|
| 1 | Ibbs |
| 2 | Nina |
My desired output for post = 2122 and reviewer = 2
| id | display_name |
|----|--------------|
| 1 | Ibbs |
| 2 | Nina |
| 3 | rakib |
My desired output for post = 2122 and reviewer = 1
| id | display_name |
|----|--------------|
| 1 | Ibbs |
| 3 | rakib |
I have tried the following query but my output is empty:
SELECT wp_invite.user_id, wp_users.display_name, wp_invite.post_id
FROM wp_invite
INNER JOIN wp_users ON wp_invite.user_id = wp_users.id
WHERE (status = 'accepted')
AND user_id NOT IN (SELECT reviewer FROM wp_rating)
AND user_id NOT IN (SELECT reviewed FROM wp_rating)
AND post_id NOT IN (SELECT post FROM wp_rating)
ID should be capitalized:
inner join wp_users on wp_invite.user_id = wp_users.ID
How to get count of combinations from database?
I have to database tables and want to get the count of combinations. Does anybody know how to put this in a database query, therefore I haven't a db request for each trip?
Trips
| ID | Driver | Date |
|----|--------|------------|
| 1 | A | 2015-12-15 |
| 2 | A | 2015-12-16 |
| 3 | B | 2015-12-17 |
| 4 | A | 2015-12-18 |
| 5 | A | 2015-12-19 |
Passengers
| ID | PassengerID | TripID |
|----|-------------|--------|
| 1 | B | 1 |
| 2 | C | 1 |
| 3 | D | 1 |
| 4 | B | 2 |
| 5 | D | 2 |
| 6 | A | 3 |
| 7 | B | 4 |
| 8 | D | 4 |
| 9 | B | 5 |
| 10 | C | 5 |
Expected result
| Driver | B-C-D | B-D | A | B-C |
|--------|-------|-----|---|-----|
| A | 1 | 2 | - | 1 |
| B | - | - | 1 | - |
Alternative
| Driver | Passengers | Count |
|--------|------------|-------|
| A | B-C-D | 1 |
| A | B-D | 2 |
| A | B-C | 1 |
| B | A | 1 |
Has anybody an idea?
Thanks a lot!
Try this:
SELECT Driver, Passengers, COUNT(*) AS `Count`
FROM (
SELECT t.ID, t.Driver,
GROUP_CONCAT(p.PassengerID
ORDER BY p.PassengerID
SEPARATOR '-') AS Passengers
FROM Trips AS t
INNER JOIN Passengers AS p ON t.ID = p.TripID
GROUP BY t.ID, t.Driver) AS t
GROUP BY Driver, Passengers
The above query will produce the alternative result set. The other result set can only be achieved using dynamic sql.
Demo here
I have searched and gone through the available topics similar to mine. But, failed to find that satisfies my requirements. Hence, posting it here.
I have four tables as follows:
"Organization" table:
--------------------------------
| org_id | org_name |
| 1 | A |
| 2 | B |
| 3 | C |
"Members" table:
----------------------------------------------
| mem_id | mem_name | org_id |
| 1 | mem1 | 1 |
| 2 | mem2 | 1 |
| 3 | mem3 | 2 |
| 4 | mem4 | 3 |
"Resource" table:
--------------------------------
| res_id | res_name |
| 1 | resource1 |
| 2 | resource2 |
| 3 | resource3 |
| 4 | resource4 |
"member-resource" table:
--------------------------------------------
| sl_no | mem_id | res_id |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 4 | 3 |
| 5 | 3 | 4 |
| 6 | 2 | 3 |
| 7 | 4 | 3 |
I want to find out the total number of distinct resources according to organizations. Expected output is as follows:
| org_name | Total Resources |
| A | 3 |
| B | 1 |
| C | 1 |
I also want to find out the total number of shared resources according to organizations. Expected output is as follows:
| org_name | Shared Resources |
| A | 1 |
| B | 0 |
| C | 1 |
Any help in this regard will highly be appreciated.
Regards.
It is much simpler than you think, particularly because you don't even need the resource table:
SELECT o.org_name, COUNT(DISTINCT mr.res_id) TotalResources
FROM member_resource mr
JOIN members m ON mr.mem_id = m.mem_id
JOIN organization o ON m.org_id = o.org_id
GROUP BY o.org_id
Output:
| ORG_NAME | TOTALRESOURCES |
|----------|----------------|
| A | 3 |
| B | 1 |
| C | 1 |
Fiddle here.
Try this query below.
SELECT org_name, COUNT(DISTINCT res_id)
FROM organization, members, member-resource
WHERE members.mem_id = member-resource.mem_id
AND organization.org_id = members.org_id
GROUP BY org_id, org_name
I am trying to redisplay the data from a table
currently the data in the table goes as
__________________________________________
| UserMeta |
|__________________________________________|
| id | userID | orgID | keyType | keyData |
|____|________|_______|__________|_________|
| 1 | 1 | 1 | UserName | Scarlet |
| 2 | 1 | 1 | Age | 23 |
| 3 | 2 | 2 | UserName | Diana |
| 4 | 2 | 2 | Age | 27 |
| 5 | 3 | 1 | UserName | Kevin |
| 6 | 3 | 1 | Age | 18 |
|____|________|_______|__________|_________|
I'd like to be able to format it to the following
_________________________________
| vUserMeta |
|_________________________________|
| UserID | orgID | UserName | Age |
|________|_______|__________|_____|
| 1 | 1 | Scarlet | 23 |
| 2 | 2 | Diana | 27 |
| 3 | 1 | Kevin | 18 |
|________|_______|__________|_____|
One approach would be to join UserMeta on itself as follows:
SELECT u.userID AS UserID,
u.orgID AS orgID,
u.keyData AS UserName,
v.keyData AS Age
FROM UserMeta u
JOIN UserMeta v
ON u.userID = v.userID
AND u.keyType = 'UserName'
AND v.keyType = 'Age'