MySQL join two Tables to n:m Relation - mysql

I have two tables (Table 1 and Table 2) and I want to create a table (Table 3) with the ID's. So to speak a n to m relationship table.
Table 1
1 Mark
2 George
3 David
Table 2
5 Bank
6 Construction
7 Carfactory
Table 3
1 5
1 6
1 7
2 5
2 6
2 7
3 5
3 6
3 7
I can make it so that the rows from table 1 are taken from 1 to 3 but not as to the row 1 all rows are taken from the table 2. Can anyone help me ?

You can achieve your expected output using CROSS JOIN.
SELECT table1.id,table2.id FROM table1 CROSS JOIN table2

Related

Create a Matrix-View in MySQL [duplicate]

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
MySQL - Rows to Columns
(13 answers)
Closed last month.
if have 2 tables:
tool_id
name
status
1
Mike
1
1
Claus
3
1
Max
3
2
Mike
2
2
Claus
2
3
Mike
1
3
Claus
3
3
Max
1
3
Sam
1
4
Mike
2
and
tool_id
tool_name
1
51363
2
52514
3
51458
4
51608
I need a view that shows me these 2 tables as a matrix. The tools should be displayed in the table headers and the names and their status for the corresponding tool as a table.
If a name is not assigned to a tool, it should be given a zero.
name
51363
52514
51458
51608
Mike
1
2
1
2
Claus
3
2
3
0
Max
3
0
3
0
Sam
0
0
1
0
I have more than 400 tools and workers each in the two tables.
Is something like this possible?
Edit: This is not an duplicate of the linked posts.
I need a dynamic creation of columns.

MySQL filter out subset by group by

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.
Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

mysql count and join

I want to retrieve table with total count from multiple table and joins
I have 4 wordpress custom database tables.
wp_tutorials
ID tut_name
1 php
2 mysql
3 wordpress
wp_chapters
ID tut_id chapter_name
1 1 php1
2 1 php2
3 2 mysql1
4 2 mysql2
wp_series
ID chapter_id series_name
1 1 php1A
2 1 php1B
3 2 php2A
4 2 php2B
5 3 mysql1A
6 3 mysql1B
7 4 mysql2A
8 4 mysql2B
wp_tut_users
ID series_id user_name
1 2 user1
2 2 user2
3 4 user3
4 6 user4
5 7 user5
from these four tables I want to retrieve by sql query following table.
1. tutorial
tut_name total_users
php 3
mysql 2
wordpress 0
expecting best ways...
Use a left join to get even tutorials with no users
select t.tut_name, count(u.id) as total_users
from wp_tutorials t
left join wp_chapters c on c.tut_id = t.id
left join wp_series s on s.chapter_id = c.id
left join wp_tut_users u on u.series_id = s.id
group by t.tut_name

How to get the count of rows from a table in cakephp

I have two tables:
id category status
1 test 1
2 test1 1
3 test2 1
This is the group_master table.
groupid groupname groupmaster
1 yy 1
2 xx 1
3 yyyy 1
4 xrx 1
5 yy 2
6 xx 2
7 yyyy 2
8 xfgdrx 3
This is the membergroup table.
The group_master.id is same as in membergroup.groupmaster.That menas i want to get each row from first table and also want to get the count from second table
That means:
id category status Count
1 test 1 4
2 test1 1 3
3 test2 1 1
This is the result i want to get.
How can i do this in Cakephp with pagination ?
try this:
You need to JOIN both tables and do a GROUP BY
SELECT g.id,g.category,g.status,count(*) as Count
FROM group_master g
JOIN membergroup m
ON g.id=m.groupmaster
GROUP BY g.id,g.category,g.status
SQL Fiddle Demo

I need sql query for getting the Required format

i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/