ORDER BY SQL on many tables - mysql

I have this table
Table1
id | name |
1 | a |
2 | c |
and other table like this
Table2
id | name |
1 | b |
2 | d |
How can I sort this two table to result
a b c d
DONE READING THIS but it will only gives me this result
a c b d
when I try this
SELECT * FROM Table1 AS t1 JOIN Table2 AS t2 ON t2.id = t1.id
ORDER BY t1.name ASC, t2.name ASC;

You can try taking a UNION of the two tables instead:
SELECT id, name
FROM Table1
UNION ALL
SELECT id, name
FROM Table2
ORDER BY name
If you want to retain information about the original source of each record, you can add a column for that:
SELECT id, name, 't1' AS source
FROM Table1
UNION ALL
SELECT id, name, 't2'
FROM Table2
ORDER BY name
Update:
If Codeigniter does not support UNION out of the box, you can always put the above query into a string and execute it natively:
$sql = "SELECT id, name FROM Table1 UNION ALL SELECT id, name FROM Table2 ORDER BY name";
$this->db->query($sql);

SELECT *
INTO #TEMP_TBL
FROM Table1 AS t1
JOIN Table2 AS t2
ON t2.id = t1.id
SELECT *
FROM #TEMP_TBL
ORDER BY ID, NAME
DROP TABLE #TEMP_TBL

If you want to use join then try below query
select * from (select x.* from table1 as x left join table2 as y on x.id = y.id) as z order by z.name;

We can use this also:
SELECT * FROM (
SELECT id, NAME FROM Table1
UNION ALL
SELECT id, NAME FROM Table2) t ORDER BY name

Related

SQL JOIN and count without counting an ID twice

I am trying to join a table and get a count but I cannot count an ID twice in the table for the count.
Table 1:
ID animal
-- ------
1 dog
2 dog
3 cat
4 cat
5 dog
Table 2:
ID
--
2
2
3
5
5
I need to get a count of how many of each type of animal are in table 2. I can get it to join and change the ID to the type of animal and then get a count of each.
The issue is that each ID can only get counted once. So the expected output would be.
dog:2
cat:1
Where my output is
dog:4
cat:1
Try like below
select t1.animal, count( distinct t2.ID)
from table1 t1 join table2 t2 on t1.ID=t2.ID
group by t1.animal
You can try below using count distinct id
select b.animal,count(distinct a.id) from table2 a
inner join table1 b on a.id=b.id
group by b.animal
Try this:
SELECT t1.animal AS "Animal", COUNT(DISTINCT t1.ID) AS "No. of Animals"
FROM TABLE2 t2, TABLE1 t1
WHERE t2.ID = t1.ID
GROUP BY t1.animal
You can Try Nested Selects here.
SELECT
t.animal,
COUNT(t.ID) AS Count
FROM
(SELECT DISTINCT a.animal, b.ID FROM table1 a INNER JOIN table2 b ON a.ID = b.ID)t
GROUP BY t.animal
this is tested image

MySQL select a set of columns with more than one variation in another column

table looks like this:
id group name
1 1 A
2 1 A
3 2 A
4 2 B
5 3 A
I want to select the rows with more than one distinct names in the same group. The result should be the following:
id group name
3 2 A
4 2 B
Any idea how do achieve this?
You can get the groups with aggregation:
select group
from t
group by group
having min(name) <> max(name);
You can get the original rows using join, in, or exists:
select t.*
from t
where t.group in (select group
from t
group by group
having min(name) <> max(name)
);
Note: group is a lousy name for a column because it is a SQL keyword and a MySQL reserved word.
You could do it with a correlated subquery:
SELECT t1.id, t1.group, t1.name
FROM mytable AS t1
WHERE EXISTS (
SELECT * FROM mytable t2
WHERE t2.group=t1.group AND t2.name <> t1.name
);
Or you could do it by counting distinct names in the group:
SELECT t1.id, t1.group, t2.name
FROM mytable AS t1
INNER JOIN (
SELECT t2.group FROM mytable AS t2
GROUP BY t2.group HAVING COUNT(DISTINCT t2.name) > 1
) AS t2 USING (group);

Find not common users in sql

I have 5 tables. I want to get specific users in table 1 that are not in table2, table3, table4 and table5. Can someone please help me :)
table1(userid,discount)
table2(userid,discount)
table3(userid,discount)
table4(userid,discount)
table5(userid,discount)
The following query:
SELECT userid
FROM table1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM (
SELECT userid
FROM table2
UNION
SELECT userid
FROM table3
UNION
SELECT userid
FROM table4
UNION
SELECT userid
FROM table5) AS t2
WHERE t1.userid = t2.userid)
returns all users of table1 that don't exist in any of the other tables.
Demo here
If you also want, say, all users of table2 that don't exist in any of the other tables, then you can modify the above query so as to return users from table2 and perform a UNION between these two queries:
SELECT userid
FROM table1 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM (
SELECT userid
FROM table2
UNION
SELECT userid
FROM table3
UNION
SELECT userid
FROM table4
UNION
SELECT userid
FROM table5) AS t2
WHERE t1.userid = t2.userid)
UNION ALL
SELECT userid
FROM table2 AS t1
WHERE NOT EXISTS (
SELECT 1
FROM (
SELECT userid
FROM table1
UNION
SELECT userid
FROM table3
UNION
SELECT userid
FROM table4
UNION
SELECT userid
FROM table5) AS t2
WHERE t1.userid = t2.userid)
Demo here
The above can easily be extended so as to incorporate users from all tables. But, I have to admit, it becomes quite verbose !

GROUP BY and JOIN in HIVE

How can I do something like this in HIVE:
Table1:
ID Name Friends
1 Tom 5
Table2:
ID Name DOB
1 Jerry 10/10/1999
1 Kate Null
1 Peter 02/11/1983
1 Robert Null
1 Mitchell 09/09/2000
What I want to do is:
For each ID in table 1, find num of not null DOB and then divide by Friends
I wrote a query as:
SELECT t.ID, t.Friends, COUNT(s.DOB)/ t. Friends from Table1 t join Table2 s on (t.ID = s.ID) GROUP BY t.ID
When I do this, I get the error as FRIENDS is not part of the GROUP BY Key
The answer I am expecting is: 3/5
Just add the FRIENDS to your GROUP BY section:
SELECT t.ID, t.FRIENDS, COUNT(s.DOB)/ t. FRIENDS
from Table1 t
join Table2 s
on (t.ID = s.ID)
GROUP BY t.ID. t.FRIENDS
I prefer to write this kind of query like this:
SELECT t.ID, case when t.FIREND>0 then Cnt / t.FRIENDS ELSE 0 END
FROM Table t1
JOIN (Select ID, Count(*) AS Cnt from Table 1 GROUP BY Id) t2
ON t1.ID = t2.ID
if you have declare id,friends in table 1 as integer and id in table2 as integer, then below query will get you intended output
select a.name, concat(cast(b.cnt as string),'/',cast(a.friends as string))
from table1 a
join
(select id, count(DOB) as cnt from table2 where DOB is not null group by id) b
on (a.id = b.id)

MySql Join tables

I have two tables like this.
Table 1 Fields:
CId , Name
Table 2 Fields:
CId , food
I want to get no. of food against each CId with the query "select * from table1"
Something like that should work:
SELECT
t1.* , count(t2.food) as foods
FROM
t1 LEFT JOIN t2 on (t1.Cid = t2.Cid)
GROUP BY
t2.Cid
SELECT
food
FROM
Table2 t2
JOIN Table1 t1 ON (t2.Cld = t1.Cld)
select a.name, b.food from table1 a, table1 b where a.cld = b.cld;