Is it possible to use group by for two columns where they can be group either way?
Example:
MyTable
Column1 | Column2
1 2
2 1
1 3
3 1
3 2
4 5
Result:
Column1 | Column2
1 2
1 3
3 2
4 5
As you can see, it groups the 2 columns even though interchanged. but should still get the rows that doesn't have a partner.
select
least(Column1, Column2),
greatest(Column1, Column2)
from
Table1
group by
least(Column1, Column2),
greatest(Column1, Column2)
see it working live in an sqlfiddle
read more about the functions here
Related
i have a query that its result is array like $a=[1, 3, 5]
i need another query which return records from table1 that all b values are in $a=[1,3, 5] so result for this sample is table1.id=1, 2
can i implement this with a query or i have to use php code like array_diff() to check difference between b column and $a?
**table1**
id
-----------
1 ...
2 ...
3 ...
**table 2**
table1_id b
------------
1 1
1 3
2 1
3 1
3 4
4 1
4 3
4 5
4 4
If i understand it correctly:
SELECT * FROM table2 WHERE table2.b IN (SELECT id FROM table1 WHERE YourCondition)
If you want to join multiple tables, with matching id for example, use:
Select * from table1 inner join table2 on table1.id=table2.table1_id
SELECT column1,
column2,
FROM table
ORDER BY `table`.`column1` ASC
AND `table`.`column2` ASC
You can't do this code as written above, but I would like to have column1 show 1,2,3 then it order column2 1,2,3 based on column1. How do you do this?
column1 column2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
You can write as following: column1 is enough in the order by:
Select column1, column2
from table
order by column1, column2
I have table with data like this:
Id sort name
1 1 abc
1 2 kci
2 1 asd
2 2 eww
2 3 der
2 4 acu
3 1 awq
3 2 see
3 3 eee
is it possible to create query that will give me result
Id ConcName
1 abc,kci
2 asd,eww,der,acu
3 awq,see,eee
use GROUP_CONCAT()
SELECT ID,
GROUP_CONCAT(name ORDER BY sort ASC) ConcName
FROM tableName
GROUP BY ID
SQLFiddle Demo
GROUP_CONCAT()
SELECT `id`, GROUP_CONCAT(`name` ORDER BY `sort`)
FROM `myTable`
GROUP BY `Id`
I need to delete rows from a table that have 2 columns or more in common. For ex. let's say that I need to delete rows that have last 2 columns the same.
1 1 2 3
2 2 2 2
3 2 3 3
4 7 3 3
5 4 2 2
First column is the index.
I would need to delete the 4 7 3 3 and 5 4 2 2 (since there are already 2 2 2 2 and 3 2 3 3 in)
DELETE FROM tableName
WHERE col1 NOT IN
(
SELECT minID
FROM
(
SELECT MIN(ID) minID
FROM tableName
GROUP BY col2, col3
) a
)
See SQLFiddle Demo
After some researching I found this (I think it was somewhere on stack also but can't remember the post)
SELECT * FROM tb AS t1
WHERE EXISTS (
SELECT 1 FROM tb AS t2
WHERE t2.c0= t1.c0
AND t2.c1= t1.c1
AND t2.c2= t1.c2
AND t2.id> t1.id )
I have a table like so;
id1 id2
1 1
2 1
1 2
3 2
1 3
4 3
1 4
5 4
I'd like to select it in a way that I'd get rows GROUPed by id2, but still preserving both values of id1 for the corresponding rows in the table.
So I'd get a result set like so;
id1 id1 id2
1 2 1
1 3 2
1 4 3
1 5 4
I've never been even half good in advanced database queries -- how would I go about achieving this?
If you have exactly 2 rows (with 2 values for id1) for every different value of id2, you can use this:
SELECT MIN(id1) AS id1_a
, MAX(id1) AS id1_b
, id2
FROM tableX
GROUP BY id2 ;
You could try using
SELECT id2, GROUP_CONCAT(id1) FROM your_table
GROUP BY id2
This way you have, for each id2 value, a column with all id1 values comma separated.
Take a look at GROUP_CONCAT syntax.
This might not be the perfect solution but in your case, it should work. This is a trick I used.
select id1, (sum(id1) - id1 ) as nID1, id2 from table_name group by id2
Hope it works.
Ujjwal