How to join two tables by 5 columns? Sql - php

I have two tables called customers and wantslist.
I want to join the columns customername, customeraddress , creditlimit , bytitle and byauthor.
How can I write a query that can show those column together in a table?
The first table is customers. It has the columns customeraddress , customername and creditlimit.
The second table is wantslist. It has the columns bytitle and byauthor.
How can I write a query that can join those 5 columns into 1 table?

Without knowing your specific strucutre you would use a SQL query similar to...
SELECT c.customername, c.customeraddress, c.creditlimit, w.bytitle, w. byauthor
FROM customers as c
JOIN wantslist as w on c.customerid = w.customerid

SELECT customers.customername, customers.customeraddress, customer.creditlimit, wantslist.bytitle, wantslist.byauthor
FROM wantslist
INNER JOIN customers
ON *{your matching condition}*;

Related

SQL SELECT Statement for selecting data from two tables using foreign key

I am just tinkering with SQL as I am trying to get into more complex statements. I don't know this qualifies for it or not but please guide how to go about it.
I have looked at JOINS and some question of Multiple Select Statements but unable to understand them correctly.
I have the following two tables:
emp table:
emp_id, name, address, org_id
books table:
id, emp_id, status, org_id
where emp_id in books table is foreign key referencing emp table.
I need to fetch all the records from books table of a particular org. But along with that I need to get all the data of respective employee like name, address along the result.
Please guide me in the right direction.
Thanks
Try this
select B.*,E.name,E.address from books B
inner join employee E
on B.emp_id=E.emp_id
where B.org_id=1;
I took value of of org_id as 1 for test purpose.
SELECT books.id, books.emp_id, books.status, books.org_id, emp.name, emp.address
FROM books
JOIN emp ON emp.emp_id = books.emp_id
WHERE books.org_id = '3'
As the other said, the org_id from emp table makes no sense. You must remove it from the table.
if you want all records from the books table try this
Select b.id,c.emp_id,b.status,e.name,e.address
from emp_table e
Left join books_table b
on(e.emp_id=b.emp_id)
where e.org_id='3' and b.org_id='3'
You can use this query this will gives you all book record with particular org_id along with all employee record
SELECT * FROM emp as e1
LEFT JOIN books as b1 ON e1.emp_id = b1.emp_id WHERE e1.org_id = 'YOUR_ID'
UNION ALL
SELECT * FROM emp as e1
RIGHT JOIN books as b1 ON e1.emp_id = b1.emp_id WHERE e1.org_id = 'YOUR_ID'
The following query should work for you:
SELECT b.id, b.status, b.org_id, e.emp_id, e.name, e.address
FROM Books b LEFT JOIN Employee e
ON e.emp_id = b.emp_id
WHERE b.org_id = 100
This would be the query for org 100.
It appears that your current schema is not normalized very well:
Employee: emp_id, name, address, org_id
Books: id, emp_id, status, org_id
The two tables both store the org_id, which presumably means the same thing.
This query might give you the results you want. You might want to learn more about joins in SQL. A LEFT JOIN gives you all records from one table, and matching records from another.
SELECT books.*,
emp.name AS emp_name,
emp.address AS emp_address,
emp.org_id AS emp_org_id
FROM books
LEFT JOIN emp ON books.emp_id = emp.emp_id
WHERE books.org_id=?
you can use this for all recors both tables :
select * from books
inner join emp on
books.emp_id=emp.emp_id
where books.emp_id=emp.emp_id

how to mysql join works in my result

i have two tables one is tag_names which is connected to TABLE groups by foreign key g_id.i fire 3 different select query with mysql JOIN and output is in fig(a)
here table tag_names is:
tell me please how mysql JOIN works in my result
Here's one good article about MySQL Joins: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
And keep in mind, when joining two tables on g_id, MySQL will result all the possible rows.
This result will give you all possible joins from both tables:
SELECT * FROM `tag_names`
LEFT JOIN `groups` on `groups`.`g_id` = `tag_names`.`g_id` LIMIT 5
While this one will group both tables by g_id
SELECT * FROM `tag_names`
LEFT JOIN `groups` ON `groups`.`g_id` = `tag_names`.`g_id` GROUP BY `g_id` LIMIT 5

How to get data referenced in another table in one MySQL query

I would to select some data from mysql. However, some of the data stored in the table I am querying from are in codes and to get the text description I need to reference that data to another table.
TABLE: persons
SELECT id, first_name, last_name, address_code, customer_type_code
FROM persons
WHERE id = 1001
TABLE: ref_address
SELECT address_name FROM ref_address
WHERE address_code = 123
TABLE: ref_customer_type_code
SELECT customer_type_name FROM ref_customer_type_code
WHERE customer_type_code = 456
How can I combine all three queries together to return id, first_name, last_name, address_name, customer_type_name in one query instead of querying them 3 times like this?
Please read the reference manual for join.
In short, you need to define a relation between your tables (I use aliases just to make things a bit "cheaper" to write):
select p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code
, ra.address_name
, rctc.customer_type_name
from persons as p
-- Join the persons table with the ref_address table,
-- using the address_code column of each table
inner join ref_adress as ra
on p.address_code = ra.address_code
-- Join the persons table with the ref_customer_type_code table
-- using the customer_type_code column of each table
inner join ref_customer_type_code as rctc
on p.customer_type_code = rctc.customer_type_code
where p.id = 1001
Notice that when you use multiple tables in a query it may be useful to define aliases to avoid having to write again and again the full name of the table. Also, it may be a good idea to explicitly specify each field's source table (by alias, if you are using it)
What you're looking for is a JOIN.
In a JOIN, you specify two tables and how they are related to one another. In a single SELECT statement, you can have multiple JOIN clauses.
SELECT
p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code,
a.address_name,
t.customer_type_name
FROM
persons p
JOIN ref_address a
ON p.address_code = a.address_code
JOIN ref_customer_type_code t
ON p.customer_type_code = t.customer_type_code
WHERE
p.id = 1001
This query says that the table persons and ref_address should be linked, or "joined", by the related columns address_code which are available in each table. Same goes with the tables persons and ref_customer_type_code being linked by the columns customer_type_code.

Mysql table join 2 tables

i have two tables in mySQL:
Table 1:
Club(ClubID=PK, club_name)
Table 2:
League_table(tableID=PK, position, clubID=fk, games_played, points)
how would i join the two tables to give a query that displayed only
(position, club-name, games_played)
Simple join:
select l.position, c.club_name, l.games_played
from club c, league_table l
where l.clubid=c.clubid
You are looking for a left join. ClubID is the foreign key (the column "connecting" the two tables).
select position, club_name, games_played
from league_table
left join club on club.ClubId = league_table.clubID
select a.club_name, b.position, b.games_played
from club as a join league_table as b
on a.clubid=b.clubid
Thats what you want.
#Alexen: No need of left join in this case.
#Diegoe: one friendly advise, always use on in join, without it query goes slow down when you are working on big tables.

SQL - two tables retrieving data

i try to retrieve data but i think i'm not good at sql query.
i have a table eventCategory
and one more table
This two table is created in order to use in one table
this is the initplayer table.
What i want to do retrieve data like that
30191592 Izlesene.com 7 2012-02-22 izlesene_cihan_v4
How can i do that ? pls show me the way ? How can i use these two tables?
TRY
SELECT la.id,st.profileName, la.totalEvents,la.Date,ft.eventCategory
FROM lastTable la
INNER JOIN secondTable st ON st.id=la.id
INNER JOIN firstTable ft ON ft.id = la.eventCategoryID
You didn't say the name of the second table so I will just call it T2.
select *
from T2 join
eventCategory AS e on T2.eventCategoryID = e.id
I'm assuming your second table has an 'eventCategoryID' field. If it doesn't, then I would need to know how you would relate it to the other table.

Categories