I would like to run a query to show if there is one or more than one user_id for given poll_id.
Example 1: poll_id 1106 has only user_id 1 for all it's rows.
Example 2: poll_id 1106 has more than one user_id 1 for all it's rows.
Using the example given below, this php code works:
$sql = "
SELECT 1
FROM xf_poll_vote
WHERE poll_id='1106'
having count(distinct user_id) > 1";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection);
// format results by row
while ($row = mysql_fetch_array($sql_result)) {
$user_id[] = $row["user_id"];
}
$count = count($user_id);
echo $count;
Just a COUNT / DISTINCT I think.
SELECT poll_id, CASE WHEN COUNT(DISTINCT user_id) > 1 THEN 'Many' ELSE 'One' END AS ManyOrOne
FROM SomeTable
GROUP BY poll_id
select 1
from yourTable
where poll_id = #yourPoll_id
having count(distinct user_id) > 1
if returns you anything, that poll_id have more than one user. If not, only have one or zero users
Related
I need to do an if else on a where clause.
I want to do something like
Select id from table where if(client_id = 10, client_id = 10, client_id = 1) AND foo = A
In clear I need
if client_id 10 exist where foo = A return id
else
client_id 10 doesn't exist where foo = A
use client_id = 1 where foo = A
I don't think you need an IF. You could try with this query:
SELECT id FROM table WHERE client_id in (10,1) AND foo='A';
EDIT:
Query example 1:
SELECT IF(client_id=10 AND foo='A',id,'') AS '10A',
IF(client_id <> 10 AND client_id=1 AND foo='A',id,'') AS '1A'
FROM table HAVING (10A OR 1A) <> '';
Query example 2:
SELECT id
FROM table
WHERE
client_id=CASE WHEN client_id=10 AND foo='A' THEN client_id
WHEN client_id <> 10 AND client_id=1 AND foo='A' THEN client_id
END;
Query example 3:
SELECT id
FROM table
WHERE
client_id=IF(client_id=10 AND foo='A',client_id,
IF(client_id <> 10 AND client_id=1 AND foo='A',client_id,''));
The last example could be what you initially have in mind.
I'm trying to figure out how I can query my table to see if a group of user_id's match a conversation_id.
Query 1 should return result for:
user_id 1 is looking to see if there are any conversation_id's with just user_id = 2 and user_id = 1 in it. (Should return a row for each conversation_id = 1, 2, 4, 5 based on SQL Fiddle example)
conversation_id
1
2
4
5
Query 2 should return result for:
user_id 1 is looking to see if there are any conversation_id's with user_id = 2, user_id = 1, and user_id = 4 in it. (Should return 0 rows as it doesn't exist in the SQL Fiddle example)
The table setup is located at
SQL Fiddle
You can use a combination of group by ... having and a correlated exists subquery to achieve the result you want:
-- Query 1:
SELECT
conversation_id
FROM
users_conversations uc
where not exists (
select 1 from users_conversations
where conversation_id = uc.conversation_id
and user_id not in (1,2)
)
group by conversation_id
having count(distinct user_id) = 2;
-- Query 2: same query, only different numbers.
SELECT
conversation_id
FROM
users_conversations uc
where not exists (
select 1 from users_conversations
where conversation_id = uc.conversation_id
and user_id not in (1,2,4))
group by conversation_id
having count(distinct user_id) = 3;
Sample SQL Fiddle
Note that the first query will not return 1,2,4,5 but rather 2,5 but in your sample data neither 1 or 4 has only user_id 1 and 2 as participants (conversation 1 has 1,2,3,4, and conversation 4 has 1,2,5).
If i understand it right it should be something like his.
Q1:
SELECT
CASE
WHEN
count(distinct CASE WHEN user_id in ('1','2') THEN user_id END)>='2'
THEN `conversation_id`
END 'test'
FROM
users_conversations
where 1
group by `conversation_id`
Q2:
SELECT
CASE
WHEN
count(distinct CASE WHEN user_id in ('1','2','4') THEN user_id END)>='3'
THEN `conversation_id`
END 'test'
FROM
users_conversations
where 1
group by `conversation_id`
http://sqlfiddle.com/#!9/fb29d/9
I have one table as below:
ID USER1 USER2
1 Q Y
2 W Y
3 R Y
4 T Y
5 XY Y
How I can check when USER2 column is ALL duplicate ? I'm using this code but It not working
$res = mysqli_query($conn, "SELECT COMLUMN FROM TABLE");
$result = array_unique($res);
if($result == 1 )
{
echo "Unique";
}
else
{
echo "NOT Unique";
}
Just do a:
SELECT COUNT(USER2) FROM tablename GROUP BY USER2
And see if it returns 1 record, or the first record value is equal to the total record count.
Here is an example:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT (SELECT COUNT(USER2) FROM tablename GROUP BY USER2 LIMIT 1) = (SELECT COUNT(USER2) FROM tablename)";
$result = $conn->query($sql);
if ($result->fetch_row()[0] == 1)
{
// all same
}
else
{
//not same
}
suppose you are trying to find USER2 column duplicate values, then try this query
SELECT tableName.*, COUNT(*) AS duplicate_count FROM tableName
GROUP BY USER2
HAVING duplicate_count > 1
this query return all duplicate USER2 rows
Assuming USER2 column is declared to be NOT NULL, or if it doesn't contain NULL values or if you want to ignore NULL values... then one of the easiest queries to accomplish the check would be:
SELECT COUNT(DISTINCT t.USER2) AS cnt FROM `TABLE` t
Just fetch the row returned from the query, and compare cnt to 1, if it's greater than 1, then there is more than one different non-null values.
In the more general case, when you do want to consider NULL values, then you'd likely want to use a GROUP BY query. I'd probably use a query like this:
SELECT COUNT(1)
FROM ( SELECT t.USER2 AS val FROM `TABLE` t GROUP BY t.USER2 ) s
select count(USER2) as temp from table group by USER2 having temp='1'
if any results is returned then is uniq.
You can use exists to check whether another row with a different user2 value exists. If not, then all values are the same. For larger tables using exists to check if there's are at least one pair of distinct values may be faster than getting the total # of distinct values.
select case when exists (
select 1 from mytable t1
where exists (
select 1 from mytable t2
where t2.user2 <> t1.user2
)
) then 'not unique' else 'unique'
end
You can also use DISTINCT keyword:
SELECT COUNT(USER2) - COUNT(DISTINCT USER2) FROM tableName
If result equals 0 then there is no duplicates.
I have this query:
SELECT id, name FROM users
WHERE ((id = 1) OR (5 <= 5))
This should result all records as 5 = 5, but it's not. It's only resulting records where the id = 1.
What am I doing wrong?
EDIT:
This is the full query:
SELECT project_id, project_name, project_description, project_active,
users.user_firstname, users.user_lastname FROM projects
INNER JOIN users ON projects.user_id = users.user_id
WHERE (projects.user_id = 1 || 3 <= 3)
EDIT:
Found it =/
Something was wrong with the join; user_id didn't exist anymore for some reason.
Try:
EXPLAIN EXTENDED SELECT id, name FROM users where id=1 or 5<=5;
SHOW WARNINGS;
u will find mysql executes the query is:
SELECT id,name FROM users where 1
just means (id=1 or 5<=5) is same as 1
So do you want all records smaller or equal 5?
if so its only
SELECT id, name FROM users
WHERE id <= 5
Beside that
(5 <= 5)
is "5" a table row like id?
Do this
$q = ("SELECT id, name FROM users WHERE id = 1 || 5 <= 5");
This is how I do it.
I have this schema which I need to match 2 rows from
user_data : user_id, field_id, value
A sample output would be:
user_id field_id value
-----------------------------
1 1 Gandalf
1 2 Glamdring
How do I write a query which basically say "Find the user id of the user whose field_id 1 is Gandalf, and field_id 2 is Glamdring?"
SELECT FROM looks at one row at a time. I am stumped on this. I will also need to find a solution that scale gracefully (such as looking at three rows etc.)
You could run a query to get the users that match each of the conditions and intersect the results. Since MySQL doesn't support intersect you can do it with an n-way join:
SELECT T1.user_id
FROM Table1 T1
JOIN Table1 T2 ON T1.user_id = T2.user_id
WHERE T1.field_id = 1 AND T1.value = 'Gandalf'
AND T2.field_id = 2 AND T2.value = 'Glamdring'
I would try the following:
SELECT user_id
FROM user_data
WHERE ( field_id = 1 AND value= 'Gandalf' )
OR ( field_id = 3 AND value = 'Glamdring' )
GROUP BY user_id
HAVING COUNT( field_id ) = 2
It will search for all the rows that match one of your criteria, and use GROUP BY and HAVING afterwards to find the user_id that has the expected count of matches.
select * from user_date where ( field_id= 1 AND value='Gandalf' ) OR ( field_id =2 AND value ='Glamdring' ) ;
The HAVING clause is the key. It turns the query from an "OR" statement into an "AND" statement