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.
Related
"Test" table structure
id
value
itemID
I want to check if in table "Test" there is an result with itemID = '123' and value = '456' and whether it is the last added result ORDER BY id DESC, I miss something in this code:
SELECT * FROM Test WHERE itemID = '123' AND value= '456' ORDER BY id DESC LIMIT 1
Could anyone help?
check if in table Test there is an result with itemID = '123' and value = '456' and whether it is the last added result ORDER BY id DESC
Your requirement can be litteraly translated as follows:
select *
from test t
where itemID = 123 and value = 456
and not exists (
select 1
from test t1
where t1.id > t.id
)
The NOT EXISTS condition ensures that the record being selected is the latest, id-wise.
If the requirements are not satisfied, the query returns an empty resultset.
Another way to express it is to use a correlated subquery to get the latest id:
select *
from test t
where
itemID = 123
and value = 456
and id = (select max(id) from test t)
Using Sub query in where clause you can find it.
SELECT t.* FROM `Test` as t WHERE `itemID` = 123 AND `value` = 456 AND `id` =(SELECT max(`id`) FROM Test);
SELECT (SELECT value
FROM Test
WHERE itemID = '123'
ORDER BY id DESC LIMIT 1) = '456' AS it_matches;
The result will be one of these possibilities:
1 if the last "value" is 456, or
0 if the last "value" is another non-null value, or
NULL if there are no rows with ItemID = 123 or the last row's "value" column is null.
Hi I am trying to create a mysql query that will convert multiple rows in a table to unique columns
The data I have is as follows:
The table I would like to see is as follows:
GEID|Username|First Name|Last Name|Email|Country|Dealer Code
The statement which could be used is
UPDATE table_name
SET column1 = value 1 , column 2 = value 2 ...
Where condition;
Sorry but my SQL isn't the best but hope the statement helps
This is a real pain, because you don't have an id identifying groups that are the same. In other words, you are missing the entity id.
I think you can construct one by counting the number of GEID values before any given row. The rest is just aggregation:
select max(case when fieldname = 'GEID' then fieldData end) as GEID,
max(case when fieldname = 'Username' then fieldData end) as Username,
. . .
from (select t.*,
(select count(*) from t t2 where t2.id <= t.id and t2.fieldName = 'GEID'
) as grp
from t
) t
group by grp;
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
Basically I have this query:
( SELECT * FROM tbl WHERE type = 'word1' )
UNION
( SELECT * FROM tbl WHERE type = 'word2' ) // Run this query if there are no results with type = 1
Basically I would like to run the second query only if the first hasn't any results. is it possible?
The FIRST "PreCheck" query does a count of how many records ARE of type = 1. After that, if the count is greater than 1, then return 1, otherwise return 2.
Now, THAT answer can be used in the join (which is always a single row via COUNT(*)) which will either have a 1 or 2 value. THAT value will be the second value is the EQUALITY conditon. So, if there IS an entry of 1, the result will be as if
WHERE t1.Type = 1
Thus never allowing any 2 in the test. HOWEVER, if NO entries are found, it will have a value of 2 and thus create a WHERE clause of
WHERE t1.type = 2
select t1.*
from
( select if( count(*) > 0, 1, 2 ) IncludeType
from tbl t2
where t2.type = 1 ) preCheck,
tbl t1
where
t1.type = preCheck.IncludeType
If there is an index on the "type" column, the first query should be almost instantaneous.
You could write
select * from tbl
where type = 1
union
select * from tbl
where type = 2
and not exists( select * from tble where type = 1 )
but this probably won't perform as well as just doing it in your program
It does the trick:
SELECT tbl.* FROM tbl JOIN (SELECT min(type) min_type FROM tbl WHERE type between 1 and 2 ) on min_type = type
First, it selects the lesser of these two types, if any exists, and then oins this one number table to your table. It is actually a simple filter. You can use WHERE instead of JOIN, if you want.
SELECT tbl.* FROM tbl WHERE (SELECT min(type) FROM tbl WHERE type between 1 and 2 ) = type
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