-4

I have two MYSQL tables events and users. This is my events table:

id | 8

event | camping

This is my users table:

id | 1

event | camping, kayaking

FullName| John

How do i join the two tables together and print out the FullName?

ISHAK
  • 1
  • Can you edit the question to show the database structure a little better? Cause this isn't clear – Loko Apr 11 '17 at 06:52
  • But what exactly You want to print? All people who are at event camping? Or all people who are at all events from first table with their names? – Whencesoever Apr 11 '17 at 06:52
  • I think that is all you need to see - https://dev.mysql.com/doc/refman/5.7/en/left-join-optimization.html (maybe right join, depending on your selection requirements) – Velimir Tchatchevsky Apr 11 '17 at 06:55
  • I want to print out people that join event camping @Whencesoever – ISHAK Apr 11 '17 at 06:55
  • Possible duplicate of [How to join two tables mysql?](http://stackoverflow.com/questions/3536283/how-to-join-two-tables-mysql) – Junius L Apr 11 '17 at 06:56
  • Is there really camping and kayaking in one cell? Do You add another event to user just by comma ? – Whencesoever Apr 11 '17 at 07:00
  • yes kayaking and camping are in one cell and just separated by comma @Whencesoever – ISHAK Apr 11 '17 at 07:03
  • @ISHAK i think i gave You the answer You need in answers, Maybe You don't need that ID column, if not, just delete it. Check my answer and let's talk there! – Whencesoever Apr 11 '17 at 07:11

3 Answers3

0

i think what You want to do is exactly this:

SELECT        dbo.events.event, dbo.users.id, dbo.users.FullName
FROM            dbo.events INNER JOIN
                         dbo.users ON dbo.users.event LIKE '%' + dbo.events.event + '%'

This will give You list with ID and FUllName of all people from table users that has camping in their event column.

Whencesoever
  • 2,218
  • 15
  • 26
  • that is specific for camping only? how about kayaking? i want the query to be more flexible and not only focus on one event, can you help me with that? – ISHAK Apr 11 '17 at 07:13
  • If there is any more data you want to show or You want to WHERE based on event ID, just do it, try Yourself or ask, so i can help You ; ). – Whencesoever Apr 11 '17 at 07:14
  • that is specific for camping only? how about if i want to print about kayaking? i want the query to be more flexible and not only focus on one event, can you help me with that? – ISHAK Apr 11 '17 at 07:22
  • actually this is what i want, if camping organizer want to view who join camping, it will list out who join camping, if kayaking organizer want to view who join kayaking, it will list out who join kayaking, can you help me with that? – ISHAK Apr 11 '17 at 07:37
  • i edited my answer so You should see all the records. How and where do You implement this i don't know. – Whencesoever Apr 11 '17 at 07:42
0

for example I have two tables USERS and EVENTS. My USERS table is (ID, USERNAME) and my EVENTS table is (ID,USERID, EVENT) and I want to join both tables and show full info about users and their events in PHP. My code could be

PDO::ERRMODE_EXCEPTION]); $sql = "select us.*, ev.* from events ev left outer join users us on us.ID = ev.USERID"; $recordset = $dsn->query($query); foreach($recordset as $row) ///goes your loop where your fetch all necessary fields } catch(PDOException $ex) { echo $ex->getMessage(); } catch(Exception $ex) { echo $ex->getMessage(); } ?>

`

-2
select us.*, ev.* from event ev left outer join users us on us.id (or else) = ev.id (or else)

if records from the left table are not always match records from the right table otherwise:

select us.*, ev.* from event ev inner join users us on us.id (or else) = ev.id (or else)

joining by key fields if your tables

Hope it will help

mayersdesign
  • 5,062
  • 4
  • 35
  • 47