I would like to select multi col across multi table in order to display them as only one row in MYSQL
like this example:
___________
|uId|xID|yID|
|_1_|_2_|_4_|
Into
___
|zId|
| 1 |
| 2 |
| 4 |
here is the query but col t returns null data beside all of the selected cols
SELECT `flaghsip_leader`,`clustuer_coordinator_id`,`clustuer_cocoordinator_id`,
`flagship_activity_coleader_id`,`flagship_activity_focalpoint_id`,
`output_leader_id`,`output_coleader_id` AS `t`
FROM crpcoreix.view_all_involved_users;
From what you've described, I think you'll need to use multiple select queries and join them with UNION ALL.
e.g.
SELECT uId as zId from Table1
UNION ALL
SELECT xId as zId from Table2
UNION ALL
SELECT zId from Table3
Related
This question already has answers here:
What is the difference between JOIN and UNION?
(14 answers)
Closed 3 years ago.
How can I union multiple table in mysql and order by date?
I have 3 differents tables and I want to make a query to get all data inside that and order by date
SELECT * FROM table1, table2, table3 ORDER by date DESC
TABLE 1
ID | USER ID | DATE |
2 4 2018
TABLE 2
ID | CAR ID | DATE |
3 9 2017
TABLE 3
ID | AIR ID | DATE |
4 6 2019
I expected result like this
ID | ALLID | DATE
2 4 2018
3 9 2017
4 6 2019
The correct syntax for union is union/union all. A comma in the from clause is not correct syntax . . . well, for anything.
So:
select id, allid, date from table1
union all
select id, allid, date from table2
union all
select id, allid, date from table3
order by date desc;
You would not be able to create your 2nd column ALLID, however you could use Join to create the tables, but the search results would be difficult if you are trying to search by ID. Can you give more context.
TO Join Table 1 and Table 2 together you need to have a primary key and a foreign key, so a column from table 1 and a cloumn from table 2 that both appear in table 3.
SO you can use ID from Table 1 and Date from table 2 because both are in table 3 so these are your Join accessories:
basic join: all data
SELECT * FROM table1 t1 JOIN table3 t3 JOIN table2 t2
bit more detail: (using on as a where clause to filder based on specific date)
SELECT * FROM table1 t1 JOIN table3 t3 JOIN table2 t2 ON t3.DATE=t2.DATE
How can I count the occurrence of the field/column in SQL?
Example dataset:
A
A
A
A
B
B
C
I want:
A | 4
A | 4
A | 4
A | 4
B | 2
B | 2
C | 1
Is there anyway to do it without using GROUP BY? So far all answer I get my query retuns the following:
A | 4
B | 2
C | 1
select value, count(*) from table group by value
Use HAVING to further reduce the results, e.g. only values that occur more than 3 times:
select value, count(*) from table group by value having count(*) > 3
You could use a nested sub-select for this desired result set.
If the example table name is my_table and the column called col1:
select col1,
(select count(*) from my_table where col1 = t.col1) as Count
from my_table t;
Or if you want to remove the duplicates, use the distinct statement. It removes the duplicates of your result set.
select distinct col1,
(select count(*) from my_table where col1 = t.col1) as Count
from my_table t;
TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4
You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b
Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.
I have a table with two sets of integer values. Using MySQL I want to display all of the rows that correspond to unique entries in the second column. Basically I can have duplicate A values, but only unique B values. If there are duplicates for a value in B, remove all the results with that value. If I use DISTINCT I will still get one of those duplicates which I do not want. I also want to avoid using COUNT(). Here's an example:
|_A____B_|
| 1 2 |
| 1 3 |
| 2 2 |
| 2 4 |
| 1 4 |
| 5 5 |
Will have the following Results (1,3), (5,5). Any value in B that has a duplicate is removed.
Try this
SELECT * FROM TEMP WHERE B IN (
SELECT B FROM TEMP GROUP BY B Having COUNT(B)=1
);
I know you want to avoid using COUNT() but this is the quick solution.
working fiddle here - http://sqlfiddle.com/#!9/29d16/8
Tested and works! you need atleast count(*) to count the values
select * from test where B in (
select B from test group by B having count(*)<2
)
I don't know why you want to avoid using count(), because that's what would do the trick as follows:
Let's say your table is named "mytable"
SELECT t1.A, t1.B
FROM mytable t1
JOIN (
SELECT B, count(*) AS B_INSTANCES
FROM mytable
GROUP BY B
HAVING count(*) = 1
) t2 ON t2.B = t1.B
ORDER BY t1.A, t1.B
Is it possible to select the next lower number from a table without using limit.
Eg: If my table had 10, 3, 2 , 1 I'm trying to select * from table where col > 10.
The result I'm expecting is 3. I know I can use limit 1, but can it be done without that?
Try
SELECT MAX(no) no
FROM table1
WHERE no < 10
Output:
| NO |
------
| 3 |
SQLFiddle
Try this query
SELECT
*
FROM
(SELECT
#rid:=#rid+1 as rId,
a.*
FROM
tbl a
JOIN
(SELECT #rid:=0) b
ORDER BY
id DESC)tmp
WHERE rId=2;
SQL FIDDLE:
| RID | ID | TYPE | DETAILS |
------------------------------------
| 2 | 28 | Twitter | #sqlfiddle5 |
Another approach
select a.* from supportContacts a inner join
(select max(id) as id
from supportContacts
where
id in (select id from supportContacts where id not in
(select max(id) from supportContacts)))b
on a.id=b.id
SQL FIDDLE:
| ID | TYPE | DETAILS |
------------------------------
| 28 | Twitter | #sqlfiddle5 |
Alternatively, this query will always get the second highest number based on the inner where clause.
SELECT *
FROM
(
SELECT t.col,
(
SELECT COUNT(distinct t2.col)
FROM tableName t2
WHERE t2.col >= t.col
) as rank
FROM tablename t
WHERE col <= 10
) xx
WHERE rank = 2 -- <<== means second highest
SQLFiddle Demo
SQLFiddle Demo (supports duplicate values)
If you want to get next lower number from table
you can get it with this query:
SELECT distinct col FROM table1 a
WHERE 2 = (SELECT count(DISTINCT(b.col)) FROM table1 b WHERE a.col >= b.col);
later again if you want to get third lower number you can just pass 3 in place of 2 in where clause
again if you want to get second higher number, just change the condition of where clause in inner query with
a.col <= b.col