how to merge 2 database results in one column - mysql

I have 2 columns as follow:
A | B
---|---
7 | 1
7 | 2
3 | 7
4 | 5
-------
I want to get 1 column containing (1,2,3).
Currently i'm quering this:
SELECT `A` , `B`
FROM `mytable`
WHERE `A` =7
OR `B` =7
but I'm getting 2 columns containing the number 7 in both sides A and B.
i'm sure there is a way to get what I want but I don't know how to google that!!

You could use this:
SELECT
case when A=7 then B else A end
FROM
yourtable
WHERE
7 IN (A, B)
If you want a single column, you could use this:
SELECT
GROUP_CONCAT(case when A=7 then B else A end
ORDER BY case when A=7 then B else A end)
FROM
yourtable
WHERE 7 IN (A, B)
See fiddle here.
If value of both A and B could be 7 at the same time, and you want to skip that row, you could substitute:
WHERE 7 IN (A, B)
with
WHERE A=7 XOR B=7

Not really sure what you need, but I'm guessing something like this:
SELECT A FROM mytable WHERE B=7
UNION
SELECT B FROM mytable WHERE A=7

i think you looking for this
SELECT myresult from (
SELECT A myresult FROM Table1 WHERE A IN (1,2,3)
UNION
SELECT B myresult FROM Table1 WHERE B IN (1,2,3)
)t
ORDER BY myresult
this will out put :
MYRESULT
1
2
3
SQL DEMO HERE

Try this
SELECT A as Single FROM mytable WHERE B=7 UNION
SELECT B as Single FROM mytable WHERE A=7 ORDER BY Single;

If you are expecting result as 1, 2, 3 use below.
SELECT colB as NewCol FROM tableName WHERE colA=7
UNION
SELECT colA as NewCol FROM tableName WHERE colB=7;
If you want both columns simply use
SELECT colA, colB FROM tableName WHERE colA=7 OR colB=7;
Demo at sqlfiddle

you have to try this code..
SELECT b as value FROM users WHERE a='7'
UNION All
SELECT a as value FROM users WHERE b='7'
this query must help you.

Related

Query to match different values in one column that have same value in a separate column

I am trying to write a query that will take two different values in column b and then compare it with column c to determine if the two values in column b share the same value in Column C. However i also need column A in the output as well.
For example
Column A Column B Column C
Test 1 x 12345
Test 2 y 12345
Test 3` A 12344
Test 4 D 12342
Desired Output
Column A Column B Column C
Test 1 x 12345
Test 2 y 12345
Any help would be great
I'm not sure if the values in ColumnB are significant. This query finds values of ColumnC that are repeated and then returns those rows:
select * from T where ColumnC in (
select ColumnC from T
group by ColumnC
having count(*) > 1 /* or maybe count(distinct ColumnB) > 1 */
)
try this
SELECT a.* FROM table a join table b on a.c=b.c and a.b<>b.b
The query doesn't take into account rows that have same values in c and b column.
You can add DISTINCT in the select if needed.
You can perform a JOIN like below. See a demo fiddle http://sqlfiddle.com/#!9/da525/4
select t.*
from tbl1 t join tbl1 s on t.`Column C` = s.`Column C`
and t.`Column B` <> s.`Column B`;
(OR) Using WHERE EXISTS
select t.*
from tbl1 t
where exists ( select 1 from tbl1
where `Column C` = t.`Column C`
and `Column B` <> t.`Column B`);

repeat result multiple times in mysql

I have a table having id and no field, what I really want is the result raw will be repeated no filed times, if the no field is 2 then that raw must be repeated twice in result.
this is my sample table structure:
id no
1 3
2 2
3 1
now I need to get a result like:
1 3
1 3
1 3
2 2
2 2
3 1
I tried to write mysql query to get the result like above, but failed.
You need a table of numbers to accomplish this. For just three values, this is easy:
select t.id, t.no
from t join
(select 1 as n union all select 2 union all select 3
) n
on t.no <= n.no;
This query must do what you want to achieve:
select t.id, t.no from test t cross join test y where t.id>=y.id
not completely solve your problem, but this one can help
set #i=0;
select
test_table.*
from
test_table
join
(select
#i:=#i+1 as i
from
any_table_with_number_of_rows_greater_than_max_no_of_test_table
where
#i < (select max(no) from test_table)) tmp on no >= i
order by
id desc
EDIT :
This is on SQL Server. I checked online and see that CTEs work on MySQL too. Just couldn't get them to work on SQLFiddle
Try this, remove unwanted columns
create table #temp (id int, no int)
insert into #temp values (1, 2),(2, 3),(3, 5)
select * from #temp
;with cte as
(
select id, no, no-1 nom from #temp
union all
select c.id, c.no, c.nom-1 from cte c inner join #temp t on t.id = c.id and c.nom < t.no and c.nom > 0
)
select * from cte order by 1
drop table #temp

Select by an IN clause but should behave like AND [duplicate]

This question already has answers here:
Matching all values in IN clause
(2 answers)
Closed 9 years ago.
IN clause works like OR,
select from table where id in (1, 2, 3)
ie selects in case any of the item in group matches (returns if id is 1, 2 or 3).
I need something similar, but based on two columns, and it should return only if ColumnA has all the values of ColumnB.
For eg,
ColumnA ColumnB
---------------------------
1 a
2 b
3 c
1 a
1 b
1 c
select CoumnA from table where CoumnA in every ColumnB of (a, b, c);
-> 1 --since only 1 has all a, b and c
select CoumnA from table where CoumnA in every ColumnB of (b);
-> 1 --since 1 has b
-> 2 --since 2 has b
And so on. I know in every of is not a proper keyword, I'm just trying to show an example.
I couldn't try anything since I couldn't get my head around this logic.
Try using just IN(a,b,c)
Group by id having count(distinct id) = 3.
What about this:
select t1.ColumnA from table t1 where
exists (select * from table t2 where t1.ColumnA=t2.ColumnA and t2.ColumnB='a')
and exists (select * from table t2 where t1.ColumnA=t2.ColumnA and t2.ColumnB='b')
and exists (select * from table t2 where t1.ColumnA=t2.ColumnA and t2.ColumnB='c')
group by t1.ColumnA

Adding one extra row to the result of MySQL select query

I have a MySQL table like this
id Name count
1 ABC 1
2 CDF 3
3 FGH 4
using simply select query I get the values as
1 ABC 1
2 CDF 3
3 FGH 4
How I can get the result like this
1 ABC 1
2 CDF 3
3 FGH 4
4 NULL 0
You can see Last row. When Records are finished an extra row in this format
last_id+1, Null ,0 should be added. You can see above. Even I have no such row in my original table. There may be N rows not fixed 3,4
The answer is very simple
select (select max(id) from mytable)+1 as id, NULL as Name, 0 as count union all select id,Name,count from mytable;
This looks a little messy but it should work.
SELECT a.id, b.name, coalesce(b.`count`) as `count`
FROM
(
SELECT 1 as ID
UNION
SELECT 2 as ID
UNION
SELECT 3 as ID
UNION
SELECT 4 as ID
) a LEFT JOIN table1 b
ON a.id = b.id
WHERE a.ID IN (1,2,3,4)
UPDATE 1
You could simply generate a table that have 1 column preferably with name (ID) that has records maybe up 10,000 or more. Then you could simply join it with your table that has the original record. For Example, assuming that you have a table named DummyRecord with 1 column and has 10,000 rows on it
SELECT a.id, b.name, coalesce(b.`count`) as `count`
FROM DummyRecord a LEFT JOIN table1 b
ON a.id = b.id
WHERE a.ID >= 1 AND
a.ID <= 4
that's it. Or if you want to have from 10 to 100, then you could use this condition
...
WHERE a.ID >= 10 AND
a.ID <= 100
To clarify this is how one can append an extra row to the result set
select * from table union select 123 as id,'abc' as name
results
id | name
------------
*** | ***
*** | ***
123 | abc
Simply use mysql ROLLUP.
SELECT * FROM your_table
GROUP BY Name WITH ROLLUP;
select
x.id,
t.name,
ifnull(t.count, 0) as count
from
(SELECT 1 AS id
-- Part of the query below, you will need to generate dynamically,
-- just as you would otherwise need to generate 'in (1,2,3,4)'
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
) x
LEFT JOIN YourTable t
ON t.id = x.id
If the id does not exist in the table you're selecting from, you'll need to LEFT JOIN against a list of every id you want returned - this way, it will return the null values for ones that don't exist and the true values for those that do.
I would suggest creating a numbers table that is a single-columned table filled with numbers:
CREATE TABLE `numbers` (
id int(11) unsigned NOT NULL
);
And then inserting a large amount of numbers, starting at 1 and going up to what you think the highest id you'll ever see plus a thousand or so. Maybe go from 1 to 1000000 to be on the safe side. Regardless, you just need to make sure it's more-than-high enough to cover any possible id you'll run into.
After that, your query can look like:
SELECT n.id, a.*
FROM
`numbers` n
LEFT JOIN table t
ON t.id = n.id
WHERE n.id IN (1,2,3,4);
This solution will allow for a dynamically growing list of ids without the need for a sub-query with a list of unions; though, the other solutions provided will equally work for a small known list too (and could also be dynamically generated).

How to address multiple columns as one in MySQL?

Columns a, b and c contain some values of the same nature. I need to select all the unique values. If I had just one column I'd use something like
SELECT DISTINCT a FROM mytable ORDER BY a;
but I need to treat a, b and c columns as one and gett all the unique values ever occurring among them.
As an example, let this be a CSV representation of mytable, the first row naming the columns:
a, b, c
1, 2, 3
1, 3, 4
5, 7, 1
The result of the query is to be:
1
2
3
4
5
7
UPDATE: I don't understand why do all of you suggest wrapping it in an extra SELECT? It seems to me that the answer is
(SELECT `a` AS `result` FROM `mytable`)
UNION (SELECT `b` FROM `mytable`)
UNION (SELECT `c` FROM `mytable`)
ORDER BY `result`;
isn't it?
So you want one column all with unique values from a, b and c? Try this:
(select a as yourField from d1)
union
(select b from d2)
union
(select c from d3)
order by yourField desc
limit 5
Working example
Edited after requirements changed... There you have the order by and limit you requested. Of course, you'll get only 5 records in this example
sorry i miss understood your question. here is updated query.
select a from my table
UNION
select b from my table
UNION
select c from my table
SELECT tmp.a
FROM
(SELECT column_1 AS a
FROM table
UNION
SELECT column_2 AS a
FROM table
UNION
SELECT column_3 AS a
FROM table) AS tmp
ORDER BY `tmp`.`a` ASC
try this:
SELECT b.iResult
FROM
(SELECT a as iResult FROM tableName
UNION
SELECT b as iResult FROM tableName
UNION
SELECT c as iResult FROM tableName) b
ORDER BY b.iResult
LIMIT BY 10 -- or put any number you want to limit.