I'm really having difficulty doing a join? How can i join two table that the only relation is the username. For example:
I have two tables. tableone and tabletwo each with there own respective rows and columns.
Table One
id trans_ref username amount
2 12345 peter 50
3 45678 john 30
4 8790 frank 10
Table Two
id trans_ref username recurring status company date_order amt
1 78987 peter weekly paid new lad 12/10/2015 30
2 88776 john monthly unpaid green 15/05/2015 10
3 55667 frank yearly paid blue 17/05/2015 25
how do i perform a join so that all the values will be avail to me
$stm = $pdo->....
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row['status']; //etc
}
Since both tables have a trans_ref column, you'll need to give at least one of them an alias so you can access it distinctly from the other.
SELECT t1.trans_ref AS t1_trans_ref, t1.amount, t2.*
FROM table1 AS t1
JOIN table2 AS t2 ON t1.username = t2.username
$row['trans_ref'] will be the trans_ref column from table2, $row['t1_trans_ref'] will be the trans_ref column from table1.
Related
I know that this question already asked many times here but after all I could't found my answer that what I want.
My Question is:
I have two tables and the structure of these tables is as:
table1:
item_id, store,title,available,shipping
table2:
item_id, review_rate,user_id,review_title
These tables should be join as one to many relation.
For example if the data in these tables is as:
table1:
item_id store title available shipping
-------------------------------------------------------
11 glasses ..........................
12 dresses ..........................
.
.
.
table2:
item_id review_rate user_id review_title
--------------------------------------------------
11 3 10023 good item
11 5 10024 nice item
12 1 10024 nice one
.
.
.
then the result should be as after joining:
afterJoin:
item_id store title available shipping rate people_reviewed
-----------------------------------------------------------------------
11 .................................... 4 2
12 .................................... 1 1
The query I tried to join is as:
CREATE OR REPLACE VIEW afterJoin AS
SELECT i.*,round(AVG(r.review_rate)) as rate,count(r.user_id) as people_reviewed
FROM table1 i
RIGHT JOIN table2 r ON i.item_id = r.item_id
but this return only one row.
Your query is missing a GROUP BY clause. Without it your database is aggregating all of the records together.
SELECT i.*, round(AVG(r.review_rate)) as rate, count(r.user_id) as people_reviewed
FROM table1 i
RIGHT JOIN table2 r ON i.item_id = r.item_id
GROUP BY i.item_id
The GROUP BY instructs the db to aggregate for each item_id.
I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)
In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .
I have these tables:
table 1 : attendance
-------------------------------
ID | DATE | EMPLOYEE_ID |
-------------------------------
1 2013-09-10 1
2 2013-09-10 2
3 2013-09-10 3
-------------------------------
table 2: employee
---------------
ID | NAME |
---------------
1 Smith
2 John
3 Mark
4 Kyle
5 Susan
6 Jim
---------------
My actual code to show employee option.
while($row=mysql_fetch_array($query)){
echo "<option value='$row[employee_id]'>$row[first_name] $row[last_name]</option>";
}
?>
How can i show the list of employee that not registered in table 1?
The condition is if an employee already registered in table 1, they won't appear on the option.
I want to show the list in <option>'s of <select> element. So it will return: kyle, susan, jim.
Please tell me the correct query or if there is any better option, it'll be good too. Please give some solution and explain. Thank you very much
UPDATE / EDIT:
it also based on current date, if in table 1 have no latest date e.g. today it's 2013-09-15. It will show all of employee.
You can do this with a left join and then checking for no matches:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id
where a.employee_id is null;
This is probably the most efficient option in MySQL.
EDIT:
To include a particular date, add the condition to the on clause:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id and a.date = date('2013-09-20')
where a.employee_id is null;
If I understood correctly this should work, get all employees whose ID is not in the attendance table.
SELECT * FROM employee WHERE employee.ID NOT IN
(SELECT EMPLOYEE_ID FROM attendance)
I have a table in a MySQL table called persons
id LastName FirstName
1 Hansen Timoteivn
2 Svendson Tove
3 Pettersen Kari
and another MySQL table called orders.
id OrderNo personID
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
How can I write a SQL query that I feed into PHP's mysql_query() function to return a list of "Order objects" that each contain a "Person object?" Each "Person object" has first name and last name as properties.
this query will return orders by a certain person (this will not give the object)
SELECT a.ID, a. FirstName, a.LastName, b.OrderNo
FROM Persons a INNER JOIN Orders b ON
a.ID = b.PersonID
WHERE a.ID = 1