How to pick distinct row value based on column in sql/plsql - mysql

Consider this table:
name mark1 mark2 mark3
x 1 2 2
y 2 2 2
z 1 2 3
Here, I need to select the non distinct for example consider row three, where it contains only one "2" in column2. For this how to write a SQL code? I have made it of using the count and distinct commands but not able to get it.

Try it this way
SELECT *
FROM table1
WHERE mark1 <> mark2
AND mark1 <> mark3
AND mark2 <> mark3;
Output:
| NAME | MARK1 | MARK2 | MARK3 |
|------|-------|-------|-------|
| z | 1 | 2 | 3 |
Here is SQLFiddle demo

If I understand your question enough, it would be like this:
select * from mark where (mark1<>mark2 and mark1<>mark3 and mark2<>mark3);

in case you need to select all columns contain values that are unequal
like in this case row3
SELECT *
FROM table a
WHERE a.m1<>a.m2
AND a.m2<>a.m3
AND a.m1<>a.m3
see fiddle here

Related

Convert a column of set values to a column of individual values in MySQL

I have inherited a table where one column is a comma-separated list of primary keys for a different table:
id | other_ids | value
---|-----------|-------
1 | a,b,c | 100
2 | d,e | 200
3 | f,g | 3000
I would like to convert this table to one where each other_id gets a column of its own:
id | other_id
---|---------
1 | a
1 | b
1 | c
2 | d
2 | e
3 | f
3 | g
However, I cannot think of a way to do this?
The table is > 10 GB in size, so I would like to do this inside the database, if possible.
first time post, please be kind.
Try this
select id,SUBSTRING_INDEX(other_ids,',',1) as other_id from reverseconcat
UNION
select id,SUBSTRING_INDEX(SUBSTRING_INDEX(other_ids,',',2),',',-1) as other_id from reverseconcat
UNION
select id,SUBSTRING_INDEX(SUBSTRING_INDEX(other_ids,',',3),',',-1) as other_id from reverseconcat
order by id
Although I cant really take any credit. Found this on http://www.programering.com/a/MzMyUzNwATg.html
Unsure how you will go on a huge dataset. Also you will need to add more unions if the other_ids are > 3
If you have the other table, then you can use a join and find_in_set():
select t.id, ot.pk as other_id
from t join
othertable ot
on find_in_set(ot.pk, t.other_ids) > 0;

Modify order field

I have a table like this
+----+-------+---------+
| id |order | name |
+----+-------+---------+
| 1 | 2 | John |
| 2 | 1 | William |
| 3 | 4 | Karl |
| 4 | 3 | Michael |
+----+-------+---------+
I want to Move Karl from the fourth place to the second so the order field of Karl will be set to 2, John 3 and Michael 4.
Is there a way to update the table with only one query?
You can try a conditional update:
UPDATE mytable
SET myfield = CASE other_field
WHEN 1 THEN 'value1'
WHEN 2 THEN 'value2'
WHEN 3 THEN 'value3'
END
WHERE id IN (1,2,3)
If you know that Karl is already in fourth, and you know that he's moving up, then this should do it:
UPDATE mytable
SET order = (order - 1) % 3 + 2
WHERE ORDER BETWEEN 2 AND 4
For a more reusable query, if someone is moving from position B to position A in the list (A < B), you would do this (substitute real numbers for A and B):
UPDATE mytable
SET order = (order - A + 1) % (B - A + 1) + A
WHERE ORDER BETWEEN A AND B
If you want to move someone down the list, from position A to B, instead, do this:
UPDATE mytable
SET order = (order - A - 1) % (B - A + 1) + A
WHERE ORDER BETWEEN A AND B
and everyone in between will be updated appropriately.
Thank you for your answers.
They help me come up with a single query that updates the order field from any position A to any position B:
UPDATE mytable
SET order=MOD(order-LEAST(A,B)+SIGN(A-B),GREATEST(A,B)-LEAST(A,B)+1)+LEAST(A,B)
WHERE order BETWEEN LEAST(A,B) AND GREATEST(A,B)

mysql - get number of occurrences from "different" query results

I was wondering how could I get the number of occurrences of a common string from different results (or using OR in my query, as example below).
table example:
id | name | rank
1 | name1 | 1
2 | name1 | 1
3 | name2 | 1
4 | name3 | 1
5 | name1 | 2
6 | name1 | 2
7 | name3 | 2
Now, I need to count number of occurrences for rank = 1 and rank = 2, without duplicating the count.
doing something like this:
SELECT name, COUNT(DISTINCT name)
AS name_num FROM table WHERE rank = 1 GROUP BY name;
results is
name1 | 1
name2 | 1
name3 | 1
perfect, but now I need to include some other result (i.e. rank = 1 OR rank = 2) and get the occurrences from each name, without duplicating it.
the wanted result for query example using table example and rank = 1 OR rank = 2 should be:
name1 | 2
name2 | 1
name3 | 2
I'll try to explain the result I want:
name1 is present when rank = 1 (+1) and when rank=2 (+1);
name 2 is only present when rank=1
name3 is present when rank = 1 (+1) and when rank=2 (+1);
Is it possible?
Select Name,
Count(Distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
Sql Fiddle Demo
You want COUNT(DISTINCT rank), not COUNT(DISTINCT name). Since you're grouping by name, there will only be one distinct name in each group.
SELECT name, COUNT(DISTINCT rank) name_num
FROM table
WHERE rank in (1, 2)
GROUP BY name
Select Name,
Count(distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
This is what you asked for?

MySQL: How do I add a string on row X2 with ID Y1 in column Z1 to row X1 with ID Y1 to column Z1?

TABLE A
Row IdA ValueA
1 1 ABCD
2 2 EFGH
3 3 IJKL
TABLE B
Row IdB ValueB
1 1 QWER
2 2 TYUI
3 3 OPAS
CONNECTOR X
Row IdA IdB
1 1 1
2 1 2
3 2 3
I want the output to display:
OUTPUT
Value A --- ValueB(1), ValueB(2)
ABCD --- QWER, TYUI
So, basically, every time there's a doublet in the connector table's IdA column, those two (or more) entries merge the strings in the Value field for my output.
Is this even doable with a MySQL query, or do I -have- to resort to sorting through the whole database with a PHP array? I'd rather like to avoid that, if at all possible!
I've looked at the various JOINs to no avail and thought about using a GROUP BY and COUNT(DISTINCT ...) query, but it just seems a very inelegant way to go about it. Suggestions are welcome!
SELECT a.ValueA, GROUP_CONCAT( b.ValueB SEPARATOR ', ' ) AS ValuesB
FROM connector c
JOIN tblA a ON c.IdA = a.IdA
JOIN tblB b ON c.IdB = b.IdB
GROUP BY a.IdA
Will give you results:
+--------+------------+
| ValueA | ValuesB |
+--------+------------+
| ABCD | QWER, TYUI |
| EFGH | OPAS |
+--------+------------+

MySQL query help wanted

I'm relatively new to MySQL, and it's possible that what I want may not be possible to achieve with one single query. I have the following table structure:
name | value1 | value2
I have several records where the names are the same but different values. What I would like to do is to select distinct names, but their values should be added. Here's an example:
john | 1 | 2
jane | 6 | 3
mark | 2 | 5
mark | 3 | 1
So the query (if possible) would return
john | 1 | 2
jane | 6 | 3
mark | 5 | 6
Yes, you can using group by.
Like
select name , sum(value1) as sum1, sum(value2) as sum2 from mytable group by name
You use group by with agregating functions.
SELECT name, sum(value1) as total1,sum(value2) as total2 FROM mytable GROUP BY name
SELECT name, sum(value1) as column1 ,sum(value2) as column2
FROM mytable
GROUP BY name
I recommend to learn a few lessons on MySQL.
Me as a beginner it was very helpful.
$query = "SELECT name, SUM(value1) as v1, SUM(value2) as v2 FROM table GROUP BY name";