Jump to content

Select from 2 Tables?


156418

Recommended Posts

Hi, I think there's probably an easy way to do this, but its got me a little confused.

I would like to Run a query, which selects two tables, Callbacks and TicketsSold.

Then I'd like to display the contents of Callbacks, where the cartID = cartID in TicketsSold, and where MC_EventDate equals either:
Saturday 9th December
Sunday 10th December
Saturday 16th December
Sunday 17th December
Saturday 23rd December.

I will have 5 seperate pages, so I can hard code the date into the query, but am struggling on how to put the query togther to get the necessary results. Do I need to run seperate queries on the tables then merge them somehow?

I have PHP version  4.4.2 and MySQL version 4.1.21-standard if that makes a difference

Many thanks for any pointers.
Link to comment
Share on other sites

[quote author=Vikas Jayna link=topic=117705.msg480457#msg480457 date=1165498113]
It can happen in a single query using a join:-

[code]select Callbacks.* from Callbacks,TicketsSold where Callbacks.cartID = TicketsSold.cartID and MC_EventDate in ('2006-12-09','2006-12-10','2006-12-16','2006-12-17','2006-12-23')[/code]
[/quote]

Right on... and as a side note to the initial poster, when you're dealing with advanced queries, your MySQL version is much more important than your PHP version. PHP simply initiates the query. All the brunt of the processing is handled by MySQL directly. With that being said, you should always attempt to allow your database server to do as much of the processing as possible to keep your PHP resources free for other things.
Link to comment
Share on other sites

Thanks for the help, I've got it working as I want, just have one problem.

I had to change the query slightly as the date is in the database as text rather than in Date Format. However the problem is that each record which is returned is displayed 3 times

[code]<?php
require('db_connect.php');
?>
<?php
$query = "select Callbacks.* from Callbacks,TicketsSold where Callbacks.cartId = TicketsSold.OrderID and MC_EventDate in ('Saturday 9th December')";

$results = mysql_query($query) or die(mysql_error());

?>
</head>

<body>
<p>Attendance List: Saturday 9th December 2006</p>
<p> <?php


while ($row = mysql_fetch_array($results)) {
  extract($row);
  echo "Callback Number: $CallbackNo <br>";
  echo "Customer Name: $customername <br>";
  echo "Customer Address: $customeraddress <br>";
  echo "Ticket Details: $M_disc <br>";

}

?>[/code]

Any ideas as to what I've done wrong?
Link to comment
Share on other sites

Never mind, solved it by GROUP BY now.

Many thanks for all the help, this was a system I've taken over the administration of, at least after this event i'll have the time to scrap this Database and start with a new one taking out all the multiple tables which have made it confusing!
Link to comment
Share on other sites

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.