I have a table with contents like the following:
+----+-----------+---------+--------+------+
| id | text_from | text_to | text | seen |
+----+-----------+---------+--------+------+
| 1 | A | B | Hello1 | 0 |
| 2 | X | Y | Hello2 | 1 |
| 3 | Y | X | Hello3 | 1 |
| 4 | B | A | Hello4 | 1 |
+----+-----------+---------+--------+------+
It is a conversation like A sends a text to B, B sends to A etc. How can I get the DISTINCT conversation? For example, distinct conversation between A and B, or X and Y etc.
I want to get something like
+----+-----------+---------+--------+------+
| id | text_from | text_to | text | seen |
+----+-----------+---------+--------+------+
| 1 | A | B | Hello1 | 0 |
| 2 | X | Y | Hello2 | 1 |
+----+-----------+---------+--------+------+
If once text_from and text_to has two unique values, it can not be repeated. For example, if there is text_from = A, text_to = B, the table should not have text_from = B, text_to = A.
I am trying several methods for DISTINCT and GROUP BY since a few hours, but could not figure out any solution! Any suggestions would be greatly appreciated.
Seems like a simple NOT EXISTS should do the trick. Example SQL Fiddle
select *
from table t
where not exists (
select 1
from table t1
where
(
(t.text_from = t1.text_from
and t.text_to = t1.text_to)
or (t.text_from = t1.text_to
and t.text_to = t1.text_from)
) and t.id > t1.id
)
Related
SAMPLE TABLE
TABLE FIRST_TABLE
| rid | requirements |
| 1 | 2x2 pic |
| 2 | valid id |
| 3 | 137 form |
| 4 | app form |
Second table
| id | applicant_id | rid | remarks |
| 1 | 1 | 1 | pass |
| 2 | 1 | 2 | pass |
| 3 | 2 | 1 | pass |
How to select all records from first table and show even the data is not exist on second table.
Result should be like this.
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
1 | 3 | null |
1 | 4 | null |
this is my sample code.
select requirements from first_table
left join second_table on first_table.rid = second_table.rid
where second_table.applicant_id = 1
group by first_table.rid
//result :
applicant_id | rid | remarks |
1 | 1 | pass |
1 | 2 | pass |
You just need to move the second_table.applicant_id = 1 to the join.
select requirements, first_table.rid, remarks
from first_table
left join second_table on
first_table.rid = second_table.rid and
second_table.applicant_id = 1
group by first_table.rid
http://sqlfiddle.com/#!9/a0cffdd/17
I'm developing a PHP script, and I have the following table:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 1 | 1 | 2 | -B-A-C- |
| 2 | 1 | 3 | -B-C- |
| 3 | 2 | 4 | -C-B- |
| 4 | 3 | 1 | -A-B- |
| 5 | 1 | 6 | -A-C- |
| 6 | 2 | 5 | -C-B- |
| 7 | 2 | 7 | -B-C- |
| 8 | 3 | 8 | -A- |
+----+-----------+----------+--------------+
From this table I would like to select all the rows with "feature" LIKE "%-A-%", but displaying first the result with lowest "position", then all the rows that have same value for column "id_parent" of the first result, then row with the 2nd lowest "position" and all the rows that have same "id_parent" of the result with the 2nd lowest "position", and so on...
So the final result should be:
+----+-----------+----------+--------------+
| id | id_parent | position | feature |
+----+------------+---------+--------------+
| 4 | 3 | 1 | -A-B- |
| 8 | 3 | 8 | -A- |
| 1 | 1 | 2 | -B-A-C- |
| 5 | 1 | 6 | -A-C- |
+----+-----------+----------+--------------+
For some reason I can't explain here I need to have and HAVING clause for selecting the right "feature" value (...HAVING 'feature' LIKE '%-A-%' ...).
Is it possible to make all this with MySQL (possibly without subqueries) or by processing data results with PHP?
Does this help? I've left the last part of the problem as an exercise for the reader...
SELECT a.*
, c.*
FROM my_table a
JOIN
( SELECT id_parent, MIN(position) position FROM my_table WHERE feature = 'a' GROUP BY id_parent ) b
ON b.id_parent = a.id_parent
AND b.position = a.position
JOIN my_table c
ON c.feature = a.feature
AND c.id_parent = a.id_parent;
Can someone help me tweak my SQL to get the desired result. I have a table of users, a table of cases, and a relationship table for users attached to cases.
Users Table - users
+----+-------+--------+
| id | first | last |
+----+-------+--------+
| 1 | Joe | Bloggs |
| 2 | John | Doe |
| 3 | Jane | Doe |
| 4 | Dave | Smith |
+----+-------+--------+
Case Table - cases
+----+--------+------+
| id | Case | Code |
+----+--------+------+
| 1 | Case 1 | C1 |
| 2 | Case 2 | C2 |
| 3 | Case 3 | C3 |
+----+--------+------+
Case Users Table - case_users
+----+---------+---------+
| id | case_id | user_id |
+----+---------+---------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 4 |
| 4 | 3 | 1 |
| 5 | 1 | 4 |
+----+---------+---------+
I want to query the database to return me a full list of users and return a yes or no if they are linked to case 1 and then case 2 case 3 etc.
The SQL I've got so far (selecting for Case ID 1) returns all users but is assigning 'no' to everyone:
SELECT
users.first,
users.last,
CASE WHEN case_users.case_id IS NULL THEN 'N' ELSE 'Y' END AS 'yes/no'
FROM
users
LEFT OUTER JOIN case_users ON case_users.case_id = 1
My actual result is:
+-------+--------+--------+
| First | Last | Yes/No |
+-------+--------+--------+
| Joe | Bloggs | Y |
| John | Doe | Y |
| Jane | Doe | Y |
| Dave | Smith | Y |
+-------+--------+--------+
My desired result should be:
+-------+--------+--------+
| First | Last | Yes/No |
+-------+--------+--------+
| Joe | Bloggs | Y |
| John | Doe | Y |
| Jane | Doe | N |
| Dave | Smith | Y |
+-------+--------+--------+
Can someone help me as I can't get the desired result?
You're missing the join condition from users to case_users:
SELECT
users.first,
users.last,
CASE WHEN case_users.case_id IS NULL THEN 'N' ELSE 'Y' END AS 'yes/no'
FROM
users
LEFT OUTER JOIN case_users ON users.id = case_users.user_id AND case_users.case_id = 1
You can try this one.
SELECT u.first, u.last, IF(cu.case_id,'Y','N') AS CASE_1 FROM users u
LEFT JOIN case_users cu ON u.id = cu.user_id AND cu.case_id = 1
Can use IF(cu.case_id,'Y','N')condition also instead of CASE
SELECT u.*
, CASE WHEN cu.case_id IS NULL THEN 'N' ELSE 'Y' END linked
FROM users u
JOIN cases c
LEFT
JOIN case_users cu
ON cu.user_id = u.id
AND cu.case_id = c.id
WHERE c.id = 1;
Here is my data
cardNo| userName| tablename| hours | date
1 | a | a | 12 | 12-06-2015
1 | a | a | 5 | 11-06-2015
2 | b | b | 3 | 15-06-2015
1 | a | a | 8 | 12-06-2015
2 | b | b | 3 | 21-06-2015
1 | a | a | 12 | 14-06-2015
2 | b | b | 10 | 8-06-2015
cardNo is unique. I need to display all details and total hours for each card, like:
cardNo | userName | tablename | totalhours
1 | a | a | 37
2 | b | b | 16
It's simple SUM() with GROUP BY:
SELECT cardNo,sum(hours)
FROM yourtable
GROUP BY cardNo;
I left it as an exercise for the OP to include userName and tablename columns into the query
SELECT cardNo,userName, tablename, sum(hours) hours
FROM Table_1 GROUP BY cardNo,userName,tablename
I am making a website where users can vote on which category a page is. They can vote that the page is in category a, b, c, or d.
I need to find the most commonly occurring category in the MySQL row out of all the votes.
Each time a user submits their vote, it submits the "category" that they voted for, and the "page_id".
I have this so far:
SELECT page_id, category
FROM categories
GROUP BY page_id
I cannot use a COUNT(*) WHERE category = 'a' then repeat it for each category because there is many more categories in the actual project.
If your table looks something like this:
SELECT * from categories;
+---------+----------+
| page_id | category |
+---------+----------+
| 1 | a |
| 1 | b |
| 1 | a |
| 1 | c |
| 1 | a |
| 1 | b |
| 1 | a |
| 2 | d |
| 2 | d |
| 2 | c |
| 2 | d |
| 3 | a |
| 3 | b |
| 3 | c |
| 4 | c |
| 4 | d |
| 4 | c |
+---------+----------+
17 rows in set (0.00 sec)
Then you may want to try this query:
SELECT c1.page_id, MAX(freq.total),
(
SELECT c2.category
FROM categories c2
WHERE c2.page_id = c1.page_id
GROUP BY c2.category
HAVING COUNT(*) = MAX(freq.total)
LIMIT 1
) AS category
FROM categories c1
JOIN (
SELECT page_id, category, count(*) total
FROM categories
GROUP BY page_id, category
) freq ON (freq.page_id = c1.page_id)
GROUP BY c1.page_id;
Which returns this:
+---------+-----------------+----------+
| page_id | MAX(freq.total) | category |
+---------+-----------------+----------+
| 1 | 4 | a |
| 2 | 3 | d |
| 3 | 1 | a |
| 4 | 2 | c |
+---------+-----------------+----------+
4 rows in set (0.00 sec)
Compare the results with the actual frequency distribution:
SELECT page_id, category, COUNT(*) FROM categories GROUP BY page_id, category;
+---------+----------+----------+
| page_id | category | COUNT(*) |
+---------+----------+----------+
| 1 | a | 4 |
| 1 | b | 2 |
| 1 | c | 1 |
| 2 | c | 1 |
| 2 | d | 3 |
| 3 | a | 1 |
| 3 | b | 1 |
| 3 | c | 1 |
| 4 | c | 2 |
| 4 | d | 1 |
+---------+----------+----------+
10 rows in set (0.00 sec)
Note that for page_id = 3, there is no leading frequency, in which case this query makes no guarantee on which category will be chosen in such a case.
something like
SELECT category, page_id, count(vote_id)
FROM categories
WHERE category in ('a', 'b', 'c', 'd')
GROUP BY category, page_id
ORDER BY count(vote_id) DESC
LIMIT 1
should do the trick. I assume here the votes are individually stored in a separate row per vote.
It only looks in the cqtegory you're interested in, sorts with the most votes first and only returns the first one.