473,800 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on mysql query to compare 2 tables

osward
38 New Member
Hi everyone,

I have query(s) that I need to perform which I couldn't figure out, and following is the situation

I have event table (which stores the event details) that has eid, date, time, place field, etc.
I have a member table (which stores who join which event) that has the same field eid as event table but with other member information fields.

When a member join an event, I want to check if he has already joined another event with the same date, and time in the member table. The only reference is the eid that refers back to the event table's content of time and date

The logic is he couldn't be at the different place at the same time

How am I going to construct the qurey or need more than one query to finish my task. I want to return boolean result. If the result is true, I don't allow the member to join that event and give him a warning.

The only common data both tables holds is eid and I couldn't figure out how I could accomplish my task

I could have add date and time field to the member table but I think I still have to pull out the date and time information from the event table (the event in question) and compare if there is an exist record that carries the same date and time. but I wonder if I could do it without alter any table fields.


Thanks in advance
Nov 4 '07 #1
6 2620
post
17 New Member
Hi everyone,

I have query(s) that I need to perform which I couldn't figure out, and following is the situation

I have event table (which stores the event details) that has eid, date, time, place field, etc.
I have a member table (which stores who join which event) that has the same field eid as event table but with other member information fields.

When a member join an event, I want to check if he has already joined another event with the same date, and time in the member table. The only reference is the eid that refers back to the event table's content of time and date

The logic is he couldn't be at the different place at the same time

How am I going to construct the qurey or need more than one query to finish my task. I want to return boolean result. If the result is true, I don't allow the member to join that event and give him a warning.

The only common data both tables holds is eid and I couldn't figure out how I could accomplish my task

I could have add date and time field to the member table but I think I still have to pull out the date and time information from the event table (the event in question) and compare if there is an exist record that carries the same date and time. but I wonder if I could do it without alter any table fields.


Thanks in advance
I believe I could help you figure this out, there is only one thing I need from you is a set of example data on what you are trying to accomplished. Anything will do just something I can use as reference to what you want done. Now I think the easiest way to do this would to be date & time comparisons.
Nov 4 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, osward.

How do you associate Users with events? Is there a third table that maps Users to events?
Nov 4 '07 #3
osward
38 New Member
Hi pbmods and post,

Below are tables and fields in question

A typical event rows of data of the event table ( I just listed fields in question) as follows:
[HTML]eid code name date time place
100 BNOV-07-001 FengShu 2007-11-01 18:00-20:00 Beijing
101 BNOV-07-002 Folk Culture 2007-11-01 18:00-20:00 Hong Kong[/HTML]
Actually, there are 2 user tables. One is event_user and the other is event_user_temp . They are almost identical and reason for 2 tables is, when a member register to a event, he needs to confirm the event via a email link within a time period, or his seat to the event would be offer to others.

The event_user
[HTML]jid eid user_id username code name regdate
1 100 125 osward BNOV-07-001 FengShu 2007-10-28 [/HTML]
If I had already registered and confirmed the above event BNOV-07-001 and I want to join BNOV-07-002 that happens to be the same date and time but different place, so I won't be able to be in two places at the same time, right?

This is the query I want to perform to stop this happens.

The only commod field and is unique is the eid. I perform other basic error checks by using this uid field

Hope this gives a clearer picture and task I want to perform and really appreicate help on this

The above is over my head all day and I spent lots of time trying to figure it out. The closest I could come up is using join clause in the query.
The following is from my test script:
[PHP]$eid = "100";
$user_id = "125";
$tbl_cat = "nuke_event_cat ";
$tbl_tmp = "nuke_event_use r_temp";
$tbl_usr = "nuke_event_use r";

$query ="SELECT $tbl_cat.*, $tbl_usr.user_i d $tbl_usr.userna me, $tbl_usr.regdat e FROM $tbl_cat LEFT JOIN $tbl_usr ON $tbl_cat.eid=$t bl_usr.eid WHERE $tbl_usr.user_i d=$user_id";

$result = mysql_query($qu ery) or die(mysql_error ());
while ($row = mysql_fetch_arr ay($result)) {

echo " $row[date] $row[code] $row[username] $row[regdate]<br>";[/PHP]
Yet, I could only pull information from the 2 tables namely event and user but couldn't figure out further on the WHERE part of the query

Thanks in advance
Nov 5 '07 #4
post
17 New Member
Alright I got something working here tell me if it's what you wanted to do.
[php]
<?php
link(connect);

$eid = "100";
$user_id = "125";
$tbl_cat = "nuke_event_cat ";
$tbl_tmp = "nuke_event_use r_temp";
$tbl_usr = "nuke_event_use r";

$query = "SELECT $tbl_cat.date, $tbl_usr.code, $tbl_usr.userna me, $tbl_usr.regdat e FROM $tbl_cat LEFT JOIN $tbl_usr ON $tbl_cat.eid = $tbl_usr.eid WHERE $tbl_usr.user_i d ='$user_id'";

$result = mysql_query($qu ery) or die(mysql_error ());
$dataset1 = mysql_fetch_arr ay($result);

$data_array = Array('date' => $dataset1[date], 'code' => $dataset1[code], 'username' => $dataset1[username], 'regdate' => $dataset1[regdate]);

echo("<br><pre> Dataset #1<br>");

foreach($data_a rray as $rown => $data) {
echo($rown);ech o(": ");echo($data); echo("<br>");

}

$query2 = "SELECT * FROM $tbl_cat WHERE '$dataset1[date]' AND eid != $eid";
$result_s = mysql_query($qu ery2);
$dataset2 = mysql_fetch_arr ay($result_s);

$data_array2 = Array('eid' => $dataset2[eid], 'code' => $dataset2[code], 'name' => $dataset2[name], 'date' => $dataset2[date], 'time' => $dataset2[time], 'place' => $dataset2[place]);

echo("<br><pre> Dataset #2<br>");

foreach($data_a rray2 as $rown => $data) {
echo($rown);ech o(": ");echo($data); echo("<br>");

}
if ($dataset1[date] == $dataset2[date]) {
echo("<br><pre> Error: Conflicting Date(s)");
echo("<br>Code: " . $dataset1[code] . "<br>ID: " . $eid . "<br>Date: " . $dataset1['date']);
echo("<br><br>C ode: " . $dataset2[code] . "<br>ID: " . $dataset2[eid] . "<br>Date: " . $dataset2['date'] . "<br>Time: " . $dataset2['time'] . "<br>Place: " . $dataset2['place']);
}
?>
<?php
function link ($opt) {
if ($opt == "connect") {
$link = mysql_connect(' localhost', 'login', 'password');
$db_selected = mysql_select_db ('db_name', $link);
}
if ($opt == "die") {
mysql_close();
}
}
?>
[/php]

All you need to do is edit the function link() with your database name & login/password for sql server.

What I made it do is look for the registered event date in the event_user_cat table then display all the information but it won't pull up EID 100 should get information for 101 everything you need. I hope this helps.
Nov 5 '07 #5
osward
38 New Member
Hi post, thanks for the help

I understand want you try to do with the code and tested the code on my test script.

You had missed to put the field name 'date' in your $query2 that took me quite some time to figure out what's wrong. Because it returns the first record of my event_cat table without any filtering.
That's fine because you'd done more than I expected and I do really appreicate your help. I, myself, always miss typing something that took me hours to figure out.

I think this is what I want to accomplish. I don't need to return the content itself but just number of row in the second query. If more than one than I reject the request.

Thanks again
Nov 5 '07 #6
osward
38 New Member
Hi post,

I encounter another question that I couldn't figure out

In the first query, it returns the right set of data. However, In the real world, one user might have join more than one event in different date that will still kept in the tbl_user table.
I had made myself 3 enteries in different date and the dataset1 prints out the 2008-03-01 one instead of all 3 sets of data (2 were prior to 2008-03-01, date in 2007). I couldn't find any restriction on the 1st query to limit the search[PHP]$query = "SELECT $tbl_cat.date, $tbl_usr.code, $tbl_usr.userna me, $tbl_usr.regdat e FROM $tbl_cat LEFT JOIN $tbl_usr ON $tbl_cat.eid = $tbl_usr.eid WHERE $tbl_usr.user_i d ='$user_id'";[/PHP]
As the same reason above for the 2nd query. Does it mean the 2nd query only query once and yet it should have return more than one set of data because I made totally 3 enteries at the 2008-03-01.

Maybe my initial logic doesn't stand.

Your input is highly appericated

Thanks in advance
Nov 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3074
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
0
3533
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary form for a number of platforms from our download pages at http://www.mysql.com/downloads/ and mirror sites.
11
17578
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time to load making any kind linkage with my Access data virtually useless. I have the MySQL driver setup in as a USER DSN. The MySQL data is sitting out on a server and the Access database is running locally. The network connection is very...
33
5602
by: Joshua D. Drake | last post by:
Hello, I think the below just about says it all: http://www.commandprompt.com/images/mammoth_versus_dolphin_500.jpg Sincerely, Joshua Drake
39
8434
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort of stammering and sputtering, and managed to pull out something I heard a couple of years back - that there was no real transaction safety in MySql. In flight transactions could be lost.
48
3881
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a query of all students in both tables with no duplicates. No clue whatsoever.
0
2292
by: yeahuh | last post by:
Quick and dirty version. Godaddy server using MySQL 4.0.24 I’m trying a left join to obtain id’s in table A(cars) that are NOT in car_id in table B(newspaper): *This is a cut down version to simplify testing. Full version is posted towards the end. SELECT C.id FROM cars C LEFT OUTER JOIN newspaper N USING (C.id=N.car_id) WHERE N.car_id IS NOT NULL;
6
38531
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.