I have tried to implement it with this tutorial: http://www.w3schools.com/sql/sql_join_inner.asp It is working, but I get duplicate entries. I have 3 tables and I would like to get only 2 columns. One for name and one for date. For 1 name is more date in the table. I would like to get only 1 date per name, only today.
EDIT:
The result is now looks like this:
Name Date
---- -----------
XY 2014-07-25
XY 2014-07-26
XY 2014-07-29
Z 2014-07-09
Z 2014-07-29
What I would like to get:
Name Date
---- --------
XY 2014-07-29
Z 2014-07-29
To get only the name and the date you need to use the SELECT statement (http://www.w3schools.com/sql/sql_select.asp)
INNER JOIN doesn't remove duplicates. To do that you need to use the DISTINCT statement
(http://www.w3schools.com/sql/sql_distinct.asp)
Example of your problem (NOTE: Tables are fake and might not make sense, they're just for the example)
TABLE1 TABLE2
|id | date | |id | name | age |
| 1 | 2010 | | 1 | mike | 5 |
| 2 | 2010 | | 2 | mike | 6 |
Since you have 2 people called mike that have the same date, if you join the tables you will get:
enter code here
SELECT t1.id, t1.date, t2.name, t2.age
FROM TABLE1 as t1
INNER JOIN TABLE2 as t2 ON t1.id = t2.id
TABLE JOINED
|id | date | name | age |
| 1 | 2010 | mike | 5 |
| 2 | 2010 | mike | 6 |
If you only select the date and the name, you'll get the same lines, but without the age and the id.
SELECT t1.id, t1.date, t2.name, t2.age
FROM TABLE1 as t1
INNER JOIN TABLE2 as t2 ON t1.id = t2.id
TABLE JOINED / FILTERED
| date | name |
| 2010 | mike |
| 2010 | mike |
You can see the repeated rows.
The distinct statement is for that.
The w3schools tutorial can be a little hard to follow (it's not organized in clear steps). I would recommend googling for a different one.
=========
EDIT: You changed the question.
For that you need to use GROP BY and MAX
(http://www.w3schools.com/sql/sql_groupby.asp)
(http://www.w3schools.com/sql/sql_func_max.asp)
Related
I have a problem setting up a SQL Query, hoping someone can help me.
So here's the task, I have two tables I would like to get with a single query. Not a big problem, unless it comes to WHERE clause matching. I need all entries from Table A, but only matching entries from Table B, however keeping entries from Table A where the linked ID is not existing.
To make clear what I have here is an example structure for the Tables...
TABLE A
ID | VAL1 | VAL2 | VAL3
1 | abc | xyz | 123
2 | abc | xyz | 123
3 | abc | xyz | 123
4 | abc | xyz | 123
TABLE B
ID | A-ID | X1 | X2 | X3 | FLAG
1 | 1 | ab | xy | 98 | 1
2 | 1 | ab | xy | 98 | 1
3 | 1 | ab | xy | 98 | 0
4 | 2 | ab | xy | 98 | 1
5 | 2 | ab | xy | 98 | 0
6 | 4 | ab | xy | 98 | 1
So if use this Query...
SELECT a.*, b.* FROM Table_A AS a LEFT JOIN Table_B AS b ON b.a-id = a.id WHERE b.flag = 0
... I get of course only the entries of A that have the matches in B, which would be ID 1 and 2 in this example, because 3 has no entry in B and 4 only an entry with FLAG 1.
However, in the Result-Array, I would need A3 and A4 as well, with the B Array-Values simply to be empty.
I have currenlty no clue if this can be done easily and in a single Query. I already tried a different approach by changing the query to something like...
SELECT a.*, (SELECT b.* FROM Table_B AS b WHERE b.a-id = a.id) AS array FROM Table_A AS a
... but in this case b.* is not allowed. :(
Thanks Pat for your suggestion, I just found a solution that is working for me in this case, so for anyone who might be interested, I moved the WHERE clause to the ON clause and now I get the result I needed...
SELECT a.*, b.* FROM Table_A AS a LEFT JOIN Table_B AS b ON (b.a-id = a.id AND b.flag = 0)
Need to keep that in mind next time. :)
currently working on a web project and i need help. how can i display values from 2 different tables in the database to a page? i'm still new in programming...
these are the 2 tables from database
Table A
| NAME | AGE | ID |
bryan 19 001
Table B
| current balance | date balance updated |
200 january 22, 2015
...and i want all of these to display in my web(below):
| name: | bryan |
| age: | 19 |
| id: | 001 |
| balance: | 200 |
any help is appreciated, thanks...
First that you need to do is to add foreign key in the second table. You need a field that will be pointing to the ID field in the second table:
Table B:
| current balance | date balance updated | person_id
200 january 22, 2015 001
Now you can select data from both tables using LEFT JOIN statement:
SELECT a.name, a.age, a.id, b.balance
FROM table_a as a
LEFT JOIN table_b as b ON b.person_id = a.id
JOIN is very powerful and frequently used construction. Learn more about it: http://www.w3schools.com/sql/sql_join_left.asp
I have two different tables with a similar column in both. And i need to query for all rows in table A but must exempt specific rows in table A if those rows exist in table B.
Example:
Table A
---------------------------------
item_id | item_name | price
---------------------------------
1 | apple | 100
---------------------------------
2 | banana | 150
---------------------------------
3 | ginger | 120
---------------------------------
4 | pear | 150
---------------------------------
5 | berry | 120
---------------------------------
Table B
---------------------------------
item_id | item_owner |
---------------------------------
1 | Ben |
---------------------------------
2 | Damian |
---------------------------------
3 | Greg |
---------------------------------
Based on the example above, I need to run a query to fetch all the rows in table A if item_id does not exist in table B.
The result of this query should fetch only 2 rows which are:
---------------------------------
4 | pear | 150
---------------------------------
5 | berry | 120
---------------------------------
Would ber glad to get help with this...Thank!
use LEFT JOIN
SELECT a.*
FROM tableA a
LEFT JOIN tableB b
ON a.item_id = b.item_id
WHERE b.item_id IS NULL
SQLFiddle Demo
For faster performance, you must define an INDEX on column item_id on both tables to prevent server to perform FULL TABLE SCAN.
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
SELECT *
FROM TableA
WHERE item_id NOT IN(SELECT item_id FROM tableb);
Demo.
Try this::
Select
A.*
from
A LEFT JOIN B on (A.item_id=B.item_id)
where B.item_id is null
Try this query...
SELECT A.*
FROM TableA A FULL OUTER JOIN TableB
ON A.Item_id = B.Item_ID
WHERE B.Item_ID IS NULL
I have a few tables in a MySQL database similar to this setup:
major table
---------------------
| id | name |
|-------------------|
| 0 | Architecture |
| 1 | Biology |
| 2 | Chemistry |
---------------------
college table
----------------------
| id | name |
|--------------------|
| 0 | Georgia Tech |
| 1 | Virginia Tech |
| 2 | Cal Tech |
----------------------
users table
----------------------------------------------
| id | name | major_id | college_id |
|--------------------------------------------|
| 0 | John Smith | 2 | 0 |
| 1 | Kevin Lee | 2 | 1 |
| 2 | Matt Anderson | 0 | 2 |
----------------------------------------------
Using PHP, I want to get all the information for a user using a query similar to this:
SELECT * FROM users WHERE name=`$user`
Is there someway for MySQL to automatically link the "major_id" and "college_id" columns to the "major" and "college" tables in a way where the query above would return the appropriate values?
If it is not possible with a single query, would multiple queries slow down performance considerably?
SELECT * FROM users WHERE name='$user'
This query (yours has back ticks around $user, back ticks are for column names, use double/single quotes) will only return values from the users table. You can't make MySQL "automagically" construct your joins. You have to do it explicitly, otherwise, how would you get information only from the users table if you wanted to? Use a JOIN like this:
SELECT users.name AS Username, college.name AS College, major.name AS Major
FROM users
INNER JOIN college ON users.college_id = college.id
INNER JOIN major ON users.major_id = major.id
Limit the retrieved columns by only selecting the ones you really need. So instead of the asterisk, write users.name etc.
The JOIN syntax is described in the MySQL Docs.
Joins are what your looking for, in this case your SQL would be:
SELECT `users`.`name`, `major`.`name`, `college`.`name`
FROM `users` WHERE `users`.`name`='name'
INNER JOIN `major` ON `major`.`id`=`users`.`major_id`
INNER JOIN `college` ON `college`.`id`=`users`.`college_id`
You can also alias your field names so you get something a bit more usable out:
SELECT `users`.`name` AS `applicant_name`, `major`.`name` AS `major_name`, `college`.`name` AS `college_name`
FROM `users` WHERE `users`.`name`='name'
INNER JOIN `major` ON `major`.`id`=`users`.`major_id`
INNER JOIN `college` ON `college`.`id`=`users`.`college_id`
More on Joins at http://dev.mysql.com/doc/refman/5.0/en/join.html
I have two tables a and b as follows to implement a simple block list where users can block other users.....
Table A
+------------+--------------+------+
| Name | phone |userid|
+------------+--------------+------+
| Mr Sasi | 01225 708225 | 1 |
| Miss Brown | 01225 899360 | 2 |
| Mr Black | 01380 724040 | 3 |
+------------+--------------+------+
Table B
+------------+--------------+
| blockedbyid| blockedid |
+------------+--------------+
| 1 | 2 |
| 2 | 3 |
| 1 | 3 |
+------------+--------------+
"blockedbyid" is id of user who has blocked the user in "blockedid".
I need to join the two tables and fetch all records from table A such that the result has all users who are not blocked by a particular user [ie blockedbyid='XXX'].. Can you guys give the SQL query so that i can fetch the records as a recordset??? I dont want to fetch two different rowsets and compare it in php....
Something like this should work
Parameter :USERID
SELECT * FROM TABLEA WHERE userid NOT IN (SELECT blockedid FROM TABLEB WHERE blockedbyid = :USERID)
Using join
SELECT u.* FROM TABLEB b, TABLEA u WHERE b.blockedbyid = 'XXX' AND b.blockedid = NULL
It may work like that, give it a try.
Roadie57 solutions seems better though.