If I have a table with data like this :
Col1 | Col2 | Col3
111 | a |
222 | b | 1
111 | |
And I wish to select Distinct Col1 only from my temporary table then insert into another table.But with the table above , the second "111" will be selected as well since it has different Col2 data.
Or if I select Distinct Col1 only , the other columns data will be ignored...
So is there any way to solve this problem? I wish to choose only the first inserted row of same Col1 data.
I'm using this query:
mysql_query("INSERT DELAYED INTO Tableb (Col1,Col2,Col3) SELECT DISTINCT Col1,Col2,Col3 FROM TempTable");
Thanks for any reply.
PS: I'm not sure how to specified the title according to my question...perhaps someone can help me.
Maybe something like this (this is probably what you want):
INSERT INTO Tableb
(
Col1,Col2,Col3
)
SELECT
t.Col1,
MAX(t.Col2) AS Col2,
MAX(t.Col3) AS Col3
FROM
TempTable as t
GROUP BY
t.Col1
Or if you want the MIN value. Like this:
INSERT INTO Tableb
(
Col1,Col2,Col3
)
SELECT
t.Col1,
MIN(t.Col2) AS Col2,
MIN(t.Col3) AS Col3
FROM
TempTable as t
GROUP BY
t.Col1
First you need to determine last ID per distinct value of col1, and then you select rows containing this id:
INSERT DELAYED INTO Tableb (Col1,Col2,Col3)
SELECT TempTable.Col1, TempTable.Col2, TempTable.Col3
from TempTable
inner join
(
select Col1, max(ID) ID
from TempTable
group by Col1
) c
ON TempTable.ID = c.ID
Inner query returns ID's to be inserted, so one simply joins them to original table to filter the table.
Substitute max(id) with min(id) if you want first inserted TempTable record.
mysql_query("INSERT DELAYED INTO Tableb (Col1,Col2,Col3) SELECT Col1, MAX(t.Col2) AS Col2, MAX(t.Col3) AS Col3 FROM TempTable GROUP BY Col1");
INSERT DELAYED INTO Tableb (Col1,Col2,Col3) SELECT Col1,Col2,Col3 FROM TempTable GROUP BY Col1 ORDER BY [your way of sorting here]
Does that do it?
Related
I have two tables which contains duplicate values and unique values I don't need duplicate values from both the tables I need only unique values and copy it into a new table as output.
For ex:
Table 1 data
col1
101
102
103
104
Table 2 data
col1
101
102
103
105
Output required is
New Table 3 data
col1
104
105
The query is
SELECT
Table 1 data.col1
FROM
Table 1 data
LEFT JOIN
Table 2 data
ON
Table 1 data.col1 = Table 2 data.col1
WHERE
Table 2 data.col1 is NULL
Hope this will help :
(SELECT t1.col1 FROM t1 left join t2 on t1.col1 =t2.col1 WHERE t2.col1 is null) union (SELECT t2.col1 FROM t2 left join t1 on t1.col1=t2.col1 WHERE t1.col1 is null)
Use the Below Query with UNION:
(SELECT Table1.column1 FROM Table1 left join Table2 on Table1.column1 =Table2.column1 WHERE Table2.column1 is null)
union
(SELECT Table2.column1 FROM Table2 left join Table1 on Table1.column1=Table2.column1 WHERE Table1.column1 is null)
get the distinct value from both table (use UNION) to produce
101 102 103 104 105
then get the intersect data from both table (use INNER JOIN) to produce
101 102 103
after that, subtract result 2 from result 1 (in Oracle you can use MINUS operator, but you can use the same idea to build complex query for that in mysql using NOT IN operator)
Hope this will work. I tried in Oracle Database. hopefully It will work in MYSQL also.
SELECT col1
FROM tab1 outers
WHERE NOT EXISTS
(SELECT col1 FROM tab2 inners WHERE outers.COL1 = inners.COL1)
UNION
SELECT col1
FROM tab2 outers
WHERE NOT EXISTS
(SELECT col1 FROM tab1 inners WHERE outers.COL1 = inners.COL1);
INSERT INTO Table 3 data (clo1) SELECT CombinedValue FROM ( SELECT DISTINCT col1 AS CombinedValue FROM Table 1 data UNION ALL SELECT DISTINCT col1 FROM Table 2 data ) temp GROUP BY CombinedValue HAVING COUNT(*) =1
This query worked for me :)
Thank you all for your help
I'm looking for a mysql solution to have several columns output a random field from that column.
The query I have now only selects the entire row randomly but does not randomize the separated columns.
$sql = "SELECT col1, col2, col3, col4 FROM table ORDER BY RAND() limit 4";
I tried subqueries but I'm not familiar with that so if anyone could help ...
Try this:
SELECT CASE rnd
WHEN 1 THEN col1
WHEN 2 THEN col2
WHEN 3 THEN col3
WHEN 4 THEN col4
END AS col
FROM (
SELECT col1, col2, col3, col4,
FLOOR(RAND() * 4) + 1 AS rnd
FROM mytable
ORDER BY RAND() ) AS t
Expression FLOOR(RAND() * 4) + 1 generates a random number between 1 and 4 (inclusive). The outer query uses this number to randomly pick one of the 4 columns of the table.
Demo here
Edit:
If you want to shuffle columns the you can use the following query:
SELECT CASE FIND_IN_SET(1, rnd)
WHEN 1 THEN col1
WHEN 2 THEN col2
WHEN 3 THEN col3
WHEN 4 THEN col4
END AS c1,
CASE FIND_IN_SET(2, rnd)
WHEN 1 THEN col1
WHEN 2 THEN col2
WHEN 3 THEN col3
WHEN 4 THEN col4
END AS c2,
CASE FIND_IN_SET(3, rnd)
WHEN 1 THEN col1
WHEN 2 THEN col2
WHEN 3 THEN col3
WHEN 4 THEN col4
END AS c3,
CASE FIND_IN_SET(4, rnd)
WHEN 1 THEN col1
WHEN 2 THEN col2
WHEN 3 THEN col3
WHEN 4 THEN col4
END AS c4
FROM (
SELECT col1, col2, col3, col4,
(SELECT GROUP_CONCAT(i ORDER BY RAND())
FROM (SELECT 1 AS i UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 4) t) AS rnd
FROM mytable) AS t
Demo here
If every resulting row has to be independent then there is no other way than to select 16 random rows (once for each cell in your 4x4 resulting table).
SELECT
(SELECT col1 FROM `table` ORDER BY RAND() LIMIT 1) AS col1,
(SELECT col2 FROM `table` ORDER BY RAND() LIMIT 1) AS col2,
(SELECT col3 FROM `table` ORDER BY RAND() LIMIT 1) AS col3,
(SELECT col4 FROM `table` ORDER BY RAND() LIMIT 1) AS col4
FROM `table`
LIMIT 4
I think it'd be simpler if you randomize the columns in PHP as well but as a fun challenge I thought I would do it like below.
It'll give you random 4 rows and randomize/shuffle the column values.
Fist it simply GROUP_CONCAT values 1,2,3,4 but randomize the order ..then extract the indexes of numbers using FIND_IN_SET..then selects the col values based on these indexes using ELT() function.
SELECT
ELT(FIND_IN_SET(1,rand_indexes),col1,col2,col3,col4) as col1,
ELT(FIND_IN_SET(2,rand_indexes),col1,col2,col3,col4) as col2,
ELT(FIND_IN_SET(3,rand_indexes),col1,col2,col3,col4) as col3,
ELT(FIND_IN_SET(4,rand_indexes),col1,col2,col3,col4) as col4
FROM
(SELECT col1,col2,col3,col4,
(SELECT GROUP_CONCAT(i ORDER BY RAND()) as indexes FROM
(SELECT 1 as i UNION SELECT 2 UNION SELECT 3 UNION SELECT 4)indexes
)as rand_indexes
FROM `table`
)T1
ORDER BY RAND() limit 4
sqlfiddle
UPDATE If you want to shuffle your column vertically like you have mentioned in comment, then you can use this query it.
It basically selects first column 4 rows in random order then joins with 4 random rows of second column and so on...
SELECT T1.col1,T2.col2,T3.col3,T4.col4
FROM
(SELECT col1,#order1:=#order1+1 as i
FROM (SELECT col1 FROM `table` ORDER BY RAND() LIMIT 4) O1,(SELECT #order1:=0) initialize )T1
INNER JOIN
(SELECT col2,#order2:=#order2+1 as i
FROM (SELECT col2 FROM `table` ORDER BY RAND() LIMIT 4) O1,(SELECT #order2:=0) initialize )T2
ON T1.i = T2.i
INNER JOIN
(SELECT col3,#order3:=#order3+1 as i
FROM (SELECT col3 FROM `table` ORDER BY RAND() LIMIT 4) O1,(SELECT #order3:=0) initialize )T3
ON T1.i = T3.i
INNER JOIN
(SELECT col4,#order4:=#order4+1 as i
FROM (SELECT col4 FROM `table` ORDER BY RAND() LIMIT 4) O1,(SELECT #order4:=0) initialize )T4
ON T1.i = T4.i
sqlfiddle shuffle columns vertically
This one will do but I guess I'm sure it's not performing very good:
select * from
(select col1 from table order by rand()) as a,
(select col2 from table order by rand()) as b,
(select col3 from table order by rand()) as c,
(select col4 from table order by rand()) as d
order by rand()
limit 4;
It does a cross join on all 4 tables (giving n^4 rows) and then fetches the first 4 rows of that.
Update because the OP asked about the purpose of a/b/c/d:
The a, b, etc. are just (randomly chosen) aliases for the 4 subqueries and can be seen as (temporary) table names.
They are required for the syntax to refer to the subqueries. In this particular example they are useless, but MySQL (and probably other systems as well) needs them.
In the above statement the column names are already unique, but consider the case when column names are not uniqe and you wanted to perform some operations on the columns, like so:
select concat(a.col1, ' ', c.col1), b.col2, d.col2 from
(select col1 from table order by rand()) as a,
(select col2 from table order by rand()) as b,
(select col1 from table order by rand()) as c,
(select col2 from table order by rand()) as d
where b.col2 > d.col2
order by rand()
limit 4;
Then these aliases a to d are really needed to distinguish e.g. col1 from the 1st and 3rd subselect.
However, #PaulSpiegel's answer is better than mine because I missed the limit 1 in the subqueries.
I am doing a query with union from two different tables and then on the fetch_array loop I would like to know from which table out of the two I am actually grabbing, anyway without adding a flag into the table's structures. ?
The flag doesn't need to be in the table, but can easily be in the query:
SELECT 'table1' as t, ... FROM table1
UNION
SELECT 'table2' as t, ... FROM table2
...
echo $row['t'];
You don't have to select fields from a table, you can simply "select a string literal" too.
If you have columns with identical name in both tables you could use as
SELECT table1.col1 as col1, table1.col2, table1.col3 FROM table1
UNION
SELECT table2.col1 as col4, table1.col5 FROM table2
then when you do $data = fetch_assoc($q) you will have
$data["col1"] // table1.col1
$data["col2"] // table1.col2
-----------------------------
$data["col4"] //table2.col1
I have two sql queries...
set #count:=0;
select #count:=#count+1 as SNO, col1, col2 FROM table;
I want to combine above queries into a single query. Any Help?
You can simply do this,
select #count:=#count+1 as SNO, col1, col2
FROM table, (SELECT #count:=0) r ;
Just like adding RowNumber for each row
select #rownum:=#rownum+1 ‘rank’,
p.*
from player p, (SELECT #rownum:=0) r
order by score
desc limit 10;
Adding RowNumber in MySQL
As per my understanding,you are looking for Row_Number function in this case. If this is correct, please have a look here
e.g.
Select #count := #count + 1 As SNO, col1, col2
From table ,(SELECT #count:=0) foo
may help
Also you can refer ROW_NUMBER, Partition, and Over in MySQL for more understanding on the same
Combining two queries..
SELECT t1.field1, t1.field2, t2.field1
FROM (query1) as t1, (query2) as t2
WHERE t1.field1= t2.field1
Hope this will works ...
select #count:=#count+1 as SNO, col1, col2 FROM table, (SELECT #count:=0) t;
Suppose I have columns col1, col2, col3, col4 in myTable and I need to print out the values exclusive to only one column .
So if the above looks like
col1 col2 col3 col4
s e c b
c c a s
n s e a
d d q c
Then the output should be n, q b since they are exclusive only to col1, col3 and col4 respectively.
How can I achieve this through a query in mysql php?
EDIT The duplicates dont have to be in a single row .I have changed the the table layout now to make it clear.
If you are looking for a SQL-only solution, you can do a query per column like this:
SELECT
col1 AS unique_on_col1
FROM table
WHERE col1 NOT IN (SELECT col2 FROM table)
AND col1 NOT IN (SELECT col3 FROM table)
AND col1 NOT IN (SELECT col4 FROM table)
It's possible to combine all four queries with UNION but that may not be necessary depending on what you want to do with the data. Also, this query should not perform very well with large datasets.
One slightly more compact way of getting all of them at once:
select distinct col1
from myTable
where col1 not in (select a.col1
from myTable a join myTable b
on a.col1 = b.col2 or a.col1=b.col3 or a.col1=b.col4)
union
select distinct col2
from myTable
where col2 not in (select a.col2
from myTable a join myTable b
on a.col2 = b.col1 or a.col2=b.col3 or a.col2=b.col4)
union
select distinct col3
from myTable
where col3 not in (select a.col3
from myTable a join myTable b
on a.col3 = b.col1 or a.col3=b.col2 or a.col3=b.col4)
union
select distinct col4
from myTable
where col4 not in (select a.col4
from myTable a join myTable b
on a.col4 = b.col1 or a.col4=b.col2 or a.col4=b.col3)
$sql = "
SELECT DISTINCT 'col1' as descr,col1 as val FROM myTable
UNION
SELECT DISTINCT 'col2' as descr,col2 as val FROM myTable
UNION
SELECT DISTINCT 'col3' as descr,year as val FROM myTable";
$result = #mysql_query($sql, $con) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$make_array[$row['descr']][]=$row['val'];
}
I'm guessing this should work. Dint try the code out but give this one a shot and let us know.
Try This:
SELECT col1 AS unique_on_col1
FROM table
WHERE col1 NOT IN (SELECT col2 FROM table)
AND col1 NOT IN (SELECT col3 FROM table)
AND col1 NOT IN (SELECT col4 FROM table)