How to merge to MySQL tables based on id - php

Let's say I have three tables like these:
table name:
id | name
1 | Bob
2 | Alice
3 | Bryan
etc
table pants:
id | size
1 | S
3 | M
table skirt:
id | size
2 | M
How do I merge the three tables using MySQL and obtain a table like this:
result table:
id | name | pants | skirt
1 | Bob | S |
2 | Alice| | M
3 | Bryan| M |
When there's no matching id, the cell will just be blank.

Join the tables with a left join. Like this:
SELECT
tablename.id,
tablename.name,
tablepants.size AS pants,
tableskirt.size as skirt
FROM
tablename
LEFT JOIN tablepants
on tablename.id=tablepants.id
LEFT JOIN tableskirt
ON tablename.id=tableskirt.id

Related

How to select values from two different tables in SQL

I have two tables in my SQL Server database. The first is catgeories and second is products. There is a column categories_id in both tables.
HERE IS MY TABLE FOR CATEGORIES :
+----+----------+----------+
| id | category | parent |
+----+----------+----------+
| 1 | BOY | 0 |
| 2 | GIRL | 0 |
| 3 | SHIRT | 1 |
| 4 | SKIRT | 2 |
| 5 | JACKET | 1 |
+----+----------+----------+
TABLE : PRODUCTS
+-------+--------------+----------------------+
| id | title |PRICE | Categories |
+-------+--------------+------+---------------+
| 1 | RED SHIRT | 300 | 3 |
| 2 | blue SKIRT | 500 | 4 |
| 3 | jeans jacket | 500 | 3 |
+-------+--------------+------+-----+---------+
Now I want to select the values from Products table for a particular category like BOY.
Try This:
SELECT pr.id,pr.title,pr.price from products AS pr
INNER JOIN CATEGORIES AS cat ON cat.id=pr.Categories
WHERE cat.category='Boy';
SELECT products.* FROM products INNER JOIN categories ON products.categories = categories.id WHERE categories.category LIKE '%BOY%';
Use this query..
Either
SELECT * FROM tproducts WHERE categories = 1
or
SELECT * FROM tproducts
JOIN tcategories ON tcategories.id = tproducts.categories WHERE tcategories.category = 'BOY'
I don't know your table names so I just used tproducts and tcategories
There is no boy(1) ID in Categories(field) in PRODUCTS(table)
SELECT * FROM CATEGORIES
INNER JOIN PRODUCTS
ON CATEGORIES.id=PRODUCTS.Categories
WHERE CATEGORIES.category ='BOY'
FOR this use join query Example:
select *(or you can get any column as you write name) from table1 join table2 on table1.id=table2.id where table1.category='BOY';

Can this be done in a single query?

I have 3 tables in my database, in which team1 and team2 ids in Matches Table are equal to team_id in TeamNames Table.
also group in Matches Table is equal to group_id in GroupNames Table
Matches Table
-----------------------------------
| team1 | team2 | group | count |
| 3 | 5 | 1 | 1 |
| 1 | 2 | 3 | 0 |
-----------------------------------
GroupNames Table
-----------------------
| group_id | name |
| 1 | Finals |
| 3 | Semi-Final |
-----------------------
TeamName Table
-----------------------
| team_id | name |
| 5 | Flowers |
| 2 | Rainbow |
-----------------------
What I need to get is:
SELECT team1 , team1_name , team2 , team2_name , group , group_name WHERE count=1
I tried joining tables, but as each of the team1 and team2 should be related to unique id in TeamName Table I failed, getting group name was easy, but I failed getting all the above in single query
Questions:
Is this possible in single query?
Can this be done using CodeIgniter's "Active Record Class"?
Answer for q1:yes
Answer for q2:yes
Hope this may help you
$this->db->from('Matches m');
$this->db->select('m.team1,m.team2,m.group,m.count,tn1.name team1_name,tn2.name team2_name,gn.name group_name');
$this->db->join('TeamName tn1','tn1.team_id = m.team1');
$this->db->join('TeamName tn2','tn2.team_id = m.team2');
$this->db->join('GroupNames gn','gn.group_id = m.group');
$this->db->where('m.count',1);
$results=$this->db->get()->result();
yes. with INNER JOIN.
Here is an example to combine your three tables, if you want you can combine more.
SELECT * FROM matches ma INNER JOIN groups gr ON ma.groupid = gr.groupid INNER JOIN teams te ON ma.teamid = te.teamid WHERE ma.count = 1
Good luck!
Martin

Performing Searches over Many-to-Many Relationships

I have a database which (for the purposes of this example), has two tables that have a many to many association (with an intermediary table for holding the associations). Here is there structure:
Table A:
+-----+-------+-------+-------+
| aID | aCol1 | aCol2 | aCol3 |
+-----+-------+-------+-------+
| 1 | foo | aoo | doo |
+-----+-------+-------+-------+
| 2 | bar | aar | dar |
+-----+-------+-------+-------+
| 3 | baz | aaz | daz |
+-----+-------+-------+-------+
Table B:
+-----+-------+
| bID | bCol1 |
+-----+-------+
| 1 | alice |
+-----+-------+
| 2 | bob |
+-----+-------+
Association Table:
+-----+-----+
| aID | bID |
+-----+-----+
| 1 | 1 |
+-----+-----+
| 2 | 2 |
+-----+-----+
| 3 | 1 |
+-----+-----+
If I want to search for information by aCol2 LIKE 'aa%' AND the row has an association to bCol1 = 'bob' (i.e. resulting in only row aID = 2), how could I assemble a MySQL Query that could do something similar?
p.s. Sorry for the poor clarity, I am not exactly sure of the wording, but in a nut shell, it is about searching for data from one record that (for the purposes of this) has a 1-* relationship via a connecting table to a number of records, by information that exists in the entire set
SELECT
a.*
FROM
table_b b
INNER JOIN associations ab ON (b.b_id = ab.b_id)
INNER JOIN table_a a ON (ab.a_id = a.a_id)
WHERE
b.col_1 = 'bob'
AND a.col_2 LIKE 'aa%'
It's been awhile, but I believe this should work:
SELECT
*
FROM
A,
B,
associations
WHERE
A.aCol2 LIKE 'aa%' AND
A.aID = associations.aID AND
associations.bID = B.bID
You have to do 2 inner joins to combine the 3 tables.

MySQL finding rows in one table with information from another

Currently, I have two MySQL tables.
First table stores relation between the friend and his picture.
TABLE 1
id | pic_id | friend_id
----------------------------
0 | 123 | 84589
1 | 290 | 11390
2 | 884 | 84589
TABLE 2
Second table stores more information about the pic...
id | pic_id | title | color | detail
----------------------------------------------
0 | 123 | hello | black | brush
1 | 124 | world | red | paint
2 | 884 | sample | green | star
I have my friend_id and need to grab all the pic_id from Table 1 and then use the pic_id to grab the columns from Table 2(title, color, detail)...
How would I do this in MySQL?
Thank you!
Simply join the two tables.
SELECT b.title, b.color, b.detail
FROM table1 a INNER JOIN table2 b
on a.pic_id = b.pic_id
WHERE friend_id = 84589

MYSQL Joins to retrieve result

I will try to be as explanatory as possible regarding my question. I am using MYSQL/PHP to fetch data from two tables with the structure looking like the following:
table A
+---------+------------+
| userid | username |
+---------+------------+
| 1 | john |
| 2 | doe |
| 3 | lewis |
+---------+------------+
table B
+---------+------------+-----------+
| id |from_userid | to_userid |
+---------+------------+-----------+
| 1 | 1 | 3 |
| 2 | 3 | 2 |
| 3 | 1 | 2 |
| 4 | 2 | 1 |
+---------+------------+-----------+
Am trying to achieve the following:
+---------+------------+----------------------+
| id |sender username| receiver username |
+---------+------------+----------------------+
| 1 | john | lewis |
| 2 | lewis | doe |
| 3 | john | doe |
| 4 | doe | john |
+---------+------------+----------------------+
As you can see, instead of returning the sender or receiver user id, I am returning their username according to Table A.
can I use left or right joins in this scenario? Thanks in advance
Try this
SELECT b.id, sender.username AS sender_username, receiver.username AS receiver_username
FROM tableB AS b
JOIN tableA AS sender ON b.from_userid = sender.userid
JOIN tableA AS receiver ON b.to_userid = receiver.userid
Inner joins would work fine, you just need to do two of them...
Left and right joins are only needed when you want to get all records from one table and some from another table. In this case you have two distinct relationships thus the need for two joins. My MySQL skills are a bit rusty so I don't know if ' or [ are used as separators on the table/field names with spaces.
Select t1.ID, T1.userName as 'Sender userName', T2.username as 'Receiver username'
FROM [Table A] A
INNER JOIN [Table B] T1
on T1.from_userid = A.userID
INNER JOIN [Table B] T2
on T2.to_userid = A.userID

Categories