Jump to content

Recommended Posts

Hi all,

 

I've read many posts about JOIN, LEFT JOIN, RIGHT JOIN.. I guess every JOIN alternative I could get hold of, but I must admit that it confuses me.

 

What I would like to achieve is the following.

I have two tables.

 

Table one consists of user information and one field (e_id) which is relative to a post that exists in the second table.

 

I want to get ALL the fields from TABLE1 and get only three fields from TABLE2. That match that must take place is that the e_id field is equal in both tables.

 

HOW do I write this query?!

 

Thankful for any kind of help!

 

Sincerely,

Andreas

Link to comment
https://forums.phpfreaks.com/topic/142160-about-the-join-alternatives/
Share on other sites

Thank you very much!

It seems to work like I want it!

 

I experienced another interesting thing though.

 

In the first table I've used the pre-fix e_ to identify what "application" the belong too. Now, this goes also for table2 AND this makes me thinking.

 

In table1 I've got the following field: e_city

In table2 I've got the same field: e_city

 

When I do a mysql_fetch_assoc to obtain the data from my sqlQuery in PHP, HOW will I know which of these fields I'm showing, when echoing: $row["e_city"] ??

 

Am I fuzzy or clear in my description? :)

 

Thank you again for your answer!

 

Sincerely,

Andreas

Think I solved it. :)

 

Here is first table, called EVENTS.

Field	Type	Null	Key	Default	Extra
e_id	int(11)	NO	PRI	NULL	auto_increment
e_title	varchar(250)	YES	 	NULL	 
e_date	date	NO	MUL	 	 
e_starttime	time	NO	 	 	 
e_stoptime	time	NO	 	 	 
e_regstart	time	NO	 	 	 
e_regstop	time	NO	 	 	 
e_city	varchar(250)	NO	 	 	 
e_country	varchar(250)	NO	 	 	 
e_venue	varchar(250)	NO	 	 	 
e_address	varchar(250)	NO	 	 	 
e_map_lat	double	NO	 	0	 
e_map_lng	double	NO	 	0	 
e_manager	varchar(250)	NO	 	 	 
e_man_email	varchar(250)	NO	 	 	 
e_man_phone	varchar(20)	NO	 	 	 
e_num_tickets	varchar(250)	NO	 	 	 
e_max_tickets_buyer	int(5)	NO	 	 	 
e_guest_tickets	int(2)	NO	 	0	 
e_end_res_date	date	NO	 	 	 
e_last_payday	datetime	NO	 	 	 
e_reggable	tinyint(2)	NO	 	0

 

The second table, called E_TICKETBUYER:

Field	Type	Null	Key	Default	Extra
e_id	int(11)	NO	PRI	NULL	auto_increment
e_title	varchar(250)	YES	 	NULL	 
e_date	date	NO	MUL	 	 
e_starttime	time	NO	 	 	 
e_stoptime	time	NO	 	 	 
e_regstart	time	NO	 	 	 
e_regstop	time	NO	 	 	 
e_city	varchar(250)	NO	 	 	 
e_country	varchar(250)	NO	 	 	 
e_venue	varchar(250)	NO	 	 	 
e_address	varchar(250)	NO	 	 	 
e_map_lat	double	NO	 	0	 
e_map_lng	double	NO	 	0	 
e_manager	varchar(250)	NO	 	 	 
e_man_email	varchar(250)	NO	 	 	 
e_man_phone	varchar(20)	NO	 	 	 
e_num_tickets	varchar(250)	NO	 	 	 
e_max_tickets_buyer	int(5)	NO	 	 	 
e_guest_tickets	int(2)	NO	 	0	 
e_end_res_date	date	NO	 	 	 
e_last_payday	datetime	NO	 	 	 
e_reggable	tinyint(2)	NO	 	0

 

 

Hope this helps.

 

 

oh, i get it... could you also post the query you're talking about?

 

My Query looks something like:

$sqlQuery = "SELECT e_ticketbuyer.*, events.title, events.date, events.city, events.country FROM e_ticketbuyer, events WHERE events.e_id = e_ticketbuyer.e_id

 

Or is there a better way to write it?

 

But, also the main question:

When doing a mysql_fetch_assoc for a SQL query, HOW do I differ the fields based on above query?

 

Sincerely,

 

Andreas

I prefer:

 

SELECT e_ticketbuyer.*
, events.title, events.date, events.city, events.country 
FROM e_ticketbuyer
INNER JOIN events USING ( e_id )

 

About the "differ" question, the e_id values are the same, by definition, so I'm not sure I understand.  The table prefixes do not make it into the field names.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.