I have two tables.
1) jb_theme_metadata
column : idx | t_name
1 | sports
2 | movies
2) jb_theme
column : idx | theme_idx(F key of jb_theme_metadata) | u_idx (F key of jb_user)
1 | 1 | 3
2 | 1 | 4
3 | 1 | 5
4 | 2 | 7
expected output
column : idx | t_name | user_count
1 | sports | 3
2 | moives | 1
Can I make this output by a sql query not two statements?
May be this would work for you:
SELECT m.idx, m.t_name, COUNT(t.u_idx) as user_count
FROM jb_theme_metadata AS m
JOIN jb_theme AS t ON t.theme_idx=m.idx
GROUP BY t.theme_idx
Related
I have two tables
tblcities:
id | name
----------------
1 | Bahawalpur
2 | Multan
3 | Karachi
4 | Lahore
tblflights:
id | from_city_id | to_city_id
-------------------------------
1 | 1 | 2
2 | 3 | 4
3 | 2 | 1
I want to join tables so it shows the city name for both columns i.e: from_city_id & to_city_id
what i tried:
SELECT *
FROM tblflights
JOIN tblcities
ON tblflights.from_city_id = tblcities.id
result:
id | from_city_id | to_city_id | name
--------------------------------------------
1 | 1 | 2 | Bahawalpur
2 | 3 | 4 | Karachi
3 | 2 | 1 | Multan
but i want name of both cities (from_city_id & to_city_id)
i tried my best but could not found any solution.
i am using CodeIgniter
Try something like this.
SELECT tf.id, tc1.name from_city_name, tc2.name to_city_name
FROM tblflights tf
JOIN tblcities tc1
ON tf.from_city_id = tc1.id
JOIN tblcities tc2
ON tf.from_city_id = tc2.id;
You may need to remove duplicates depending on your table contents.
I have 2 tables, the first table or_f_table data. The second table or_table
or_f_table
f_id | f_o_id | f_u_id
1 | 19 | 1
2 | 5 | 2
3 | 19 | 2
or_table
o_id | o_name
4 | test1
5 | test2
19 | oops2
20 | oops3
SELECT o.o_name,
IF ((SELECT count(*) FROM or_f_table as f
WHERE f.f_u_id = 1 ),'Yes','No') as follow_status
FROM or_table as o
WHERE o.o_name LIKE '%oop%'
I want to do something like this result :-
o_name | follow_status
oops2 | Yes
oops3 | No
I am getting result
o_name | follow_status
oops2 | Yes
oops3 | Yes
Why doesn't it work? And how should I correct it
There will always be a value greater than 0 for your where condition. That is why it is not working.
Try this to get the specified results
SELECT o.o_name,
IF ((SELECT count(*) FROM or_f_table as f
WHERE f.f_o_id = o.o_id ),'Yes','No') as follow_status
FROM or_table as o
WHERE o.o_name LIKE '%oop%'
I have the following data in a Mysql table
ID | code | code order
1 | 1 | 1
1 | 2 | 2
1 | 3 | 3
2 | 1 | 1
2 | 2 | 2
2 | 3 | 3
3 | 1 | 1
3 | 4 | 2
3 | 5 | 3
4 | 1 | 1
4 | 4 | 2
4 | 5 | 3
4 | 6 | 4
How would I write a query to return the following results
code pattern 1,2,3 = 2 (count)
code pattern 1,4,5 = 1 (count)
code pattern 1,4,5,6 = 1 (count)
basically I need to find out the most popular code sequence, each sequence is grouped by an unique ID. The order the codes of the is also important. i.e
1,4,5,6 is different to 1,5,4,6
cheers
In MySQL, this is probably most easily done using two aggregation:
select pattern, count(*)
from (select id, group_concat(code order by code) as pattern
from t
group by id
) p
group by pattern;
This is my scenario
I have a permissions table with the following fields.
id | module | permission
1 | client | add
2 | client | edit
3 | client | delete
4 | someth | edit
5 | someth | delete
employee table
id | status | somestatus
1 | act | 1
2 | den | 1
3 | act | 0
4 | den | 1
5 | act | 0
6 | act | 1
Now what i would need to do is select the employee who have status="act" and somestatus=1 and give them all permissions where module="client"
so the table employee_permissions should have these rows
id | empid | permid | permvalue
1 | 1 | 1 | 1
2 | 1 | 2 | 1
3 | 1 | 3 | 1
1 | 6 | 1 | 1
2 | 6 | 2 | 1
3 | 6 | 3 | 1
This is the query I tried and I'm stuck here
INSERT INTO at2_permission_employee (employee_id,permission_id)
SELECT at2_employee.employee_id as employee_id
, (SELECT at2_permission.permission_id as permission_id
FROM at2_permission
where at2_permission.permission_module='client'
)
from at2_employee
where at2_employee.employee_status='Active'
and at2_employee.employees_served_admin = 1;
I get the error sub query returns multiple rows which makes sense to me. But I'm not sure how to modify the query to account for iterating over the rows returned by sub query
If I'm not wrong, like this:
INSERT INTO at2_permission_employee (employee_id, permission_id, permvalue)
SELECT
at2_employee.employee_id,
at2_permission.permission_id,
1
FROM at2_permission cross join at2_employee
WHERE
at2_employee.employee_status='Active'
and at2_employee.employees_served_admin = 1
and at2_permission.permission_module='client';
It's a bit unclear where the value for permvalue should come from so I hard coded it and used the permission.id for both id and permid, but this query should give you an idea on how to accomplish what you want:
insert employee_permissions (id, empid, permid, permvalue)
select p.id, e.id, p.id, 1
from employee e, permissions p
where p.module = 'client' and e.status = 'act' and e.somestatus = 1;
So lets say we have the following table of data:
A | B
_________
1 | 2
3 | 4
5 | 6
6 | 5
And what if I wanted to count the times the same numbers collide or are in the same line? So in the above example 1-2 and 3-4 would return a count of one because they are on the same line only once however 5-6 and 6-5 would return a value of 2.
A more real life illustration: think that the numbers are sport team id's and the A and B columns determine the host-team and the guest-team. Ao teams 5 and 6 have played a total of 2 games against each other, first team 5 as a host and then team 6 as a host.
So how could I count these in mysql?
DROP TABLE IF EXISTS fixtures;
CREATE TABLE fixtures
(fixture_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,home INT NOT NULL
,away INT NOT NULL
);
INSERT INTO fixtures (home,away) VALUES (1,2),(3,4),(5,6),(6,5);
SELECT * FROM fixtures;
+------------+------+------+
| fixture_id | home | away |
+------------+------+------+
| 1 | 1 | 2 |
| 2 | 3 | 4 |
| 3 | 5 | 6 |
| 4 | 6 | 5 |
+------------+------+------+
SELECT LEAST(home,away) a,GREATEST(home,away) b, COUNT(*) ttl FROM fixtures GROUP BY a,b;
+---+---+-----+
| a | b | ttl |
+---+---+-----+
| 1 | 2 | 1 |
| 3 | 4 | 1 |
| 5 | 6 | 2 |
+---+---+-----+
SELECT
CASE WHEN A < B THEN A ELSE B END AS aa,
CASE WHEN B > A THEN B ELSE A END AS bb,
COUNT(*)
FROM
Table1 t1
GROUP BY aa, bb
See it live in an sqlfiddle.
As a-b is the same as b-a, you want to normalize that result:
SELECT LEAST(a,b) AS x, GREATEST(a,b) AS y ...
Now you can count the occurences:
SELECT LEAST(a,b) AS x, GREATEST(a,b) AS y, count(*) as c FROM tablename GROUP BY x,y
Greets