I have modified the names of my Tables. The sportevents table is the main table and it should get its data from the tables: event_date, events, results, and members. Is there a way to do this. Please not i need to keep this structure.
sportevents (link table)
• id
• event_id
• date_id
• result_id
event_date
• id
• date
events
• id
• eventname
results
• id
• result
members
• id (the ID number of a person)
userlogin
• id
• username
• password
I have managed to get it right without joins. The following:
$query = "SELECT * FROM members, sportevents, dates, results, event, userlogin ".
"WHERE userlogin.username = '$un' " .
"AND sportevents.id = members.id " .
"AND sportevents.event_id = event.id " .
"AND sportevents.date_id = dates.id " .
"AND sportevents.result_id = results.id";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo $row['eventname'];
echo " - ";
echo $row['year'];
echo " - ";
echo $row['result'];
}
Gives me this:
Karoo Cycle - 2008 - 1h14mins
I presume that you have member's data stored in to $_SESSION['member'] or smth. And i dont know exactly why you have separated tabes on event, event_date (there should be one in my perspective) and I guess that main_events is something like event's group/category.
and you are missing MEMBER - EVENT link. add field 'member_id' to the events table.
If that's what it is then it's something like that.
$q = 'SELECT e.*, ed.year, r.result FROM events As e
LEFT JOIN event_date As ed ON ed.id = e.id
LEFT JOIN result As r ON r.id = e.id
WHERE e.member_id = ' . $_SESSION['member']['id'];
from that you get
event id, event name, event year, result. "JohnSmith" you can get from $_SESSION['member'].
If you decide not to use separate tables for event, event_date, result and use only one table with more fields u can do this without any LEFT JOINS just very simple SELECT query.
First, you need to link event with a result. It depends on your domain model, but I'll assume you have only one result per event, so I'll add result_id to the events table. Also, you need to link members with events somehow, I'll use
events_members
member_id
main_event_id
table. With this vcersion you'll be able to have multiple members participate in multiple events.
And finally, the query would be something like the following:
select m.username, e.eventname, y.year, r.result
from members m
join events_members em on em.member_id = m.id
join main_events me on em.main_event_id = me.id
join event e on e.id = me.event_id
join year y on y.id = me.year_id
join result r on r.id = e.result_id
where m.username = $username
Related
I have these tables:
Table that displays the results after I fetched from db:
Result table from the database:
Subjects table from db:
What I am trying to achieve: I want the student identification number appear once and each subject score to appear under the subject just like the image below. How can I go about it in my SQL queries?
My source code:
$sql = "SELECT distinct s.regiNo, s.firstName as fname, s.middleName as mname, s.lastName as lname, s.gender, s.class_group, c.subjects, e.First_CA, e.Second_CA, e.examid, e.scored, e.internaltype, e.Class, e.Year FROM student s, subjects c, exam e WHERE s.regiNo = e.Roll_Number AND e.sub_id = c.subect_code
";
Thanks.
You appears to want conditional aggregation :
select s.regiNo as IdentityNumber, e.Class, s.class_group,
sum(case when c.subect_code = 'math' then (e.First_CA + e.Second_CA + e.scored) else 0 end) as Mathematics,
. . .
from student s inner join
exam e
on s.regiNo = e.Roll_Number inner join
subjects c
on e.sub_id = c.subect_code
group by s.regiNo, e.Class, s.class_group;
I have this PHP Code:
$sql="SELECT * from client where level = '100' group by parent_client_id ";
$rs=mysql_query($sql,$pbx01_conn);
while($result=mysql_fetch_array($rs))
{
$sql2="SELECT * from customer where customerid = '".$result["parent_client_id"]."' ";
echo $sql2.'<br>';
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
if(mysql_num_rows($rs2) > 0)
{
$result2=mysql_fetch_array($rs2);
echo $result2["company"].'<br>';
}
}
I am trying to match the parent_client_id column in the client table with the customerid column in the customer table.
the customer and client tables are in 2 different databases.
I want to display the company column from the client table if there is no match between the two
can i do this using php?
SELECT company FROM db1.client LEFT JOIN db2.customer ON db1.client.parent_client_id=db2.customer.customerid
WHERE customerid IS null
Use databasename.tablename syntax if your tables are in different databases.
Try this:
SELECT c.company FROM client c INNER JOIN customer cu ON cu.customerid = c.parent_client_id WHERE c.level = '100' GROUP BY c.parent_client_id
I've been Googling for most of my day now but I can't seem to find the right answer. Maybe because I don't really know in which direction to look (join? exist?). What I have is 3 tables:
There is a table named 'groups' and 'languages' containing the actual group- and language data but that's not important right now.
The user should be able to generate a list with all members, depending on the selected groups and/or languages. The groups/languages that the user selected are saved in two separate array's containing the IDs ($aGroups and $aLangs).
What I want/need is to SELECT * FROM members WHERE ...
And that's where I got stuck. I've tried joins, I've tried IN(), I've tried EXIST but nothing seems to work right.
Any help is greatly appreciated!
If you want to select members by languages and groups, you can do something like this :
$query = "select * from members as m
left join group_members as gm on gm.member_id = m.id
left join language_members as lm on lm.member_id = m.id";
if(isset($aGroups)) {
$query .= " where group_id in (".implode(",", $aGroups).")";
if(isset($aLangs)) {
$query .= " and language_id in (".implode(",", $aLangs).")";
}
}
elseif(isset($aLangs)) {
$query .= " where language_id in (".implode(",", $aLangs).")";
}
I think you just need some brackets:
//assuming you have ids stored in $lang and $group
SELECT * FROM members WHERE (SELECT group_id FROM group_members WHERE member_id=members.id)='$group'
AND (SELECT lang_id FROM language_members WHERE member_id=members.id)='$lang'
The trick is "members.id", DB will call subquery for every member to find out if the condition for group and lang is met.
Select m.* FROM members m, group_members gm, language_members lm
WHERE
lm.member_id=m.id AND
m.id=gm.member_id AND
<<<< START YOUR OWN WHERE HERE.
This is my first question here. I have a complicated SQL database and I need to join different tables which have the same column names.
"event" is a sports match. It contains tournament_stageFK which is linked to tournament_stage table, which contains tournamentFK which is linked to tournament table, which contains tournament_templateFK which is linked to tournament_template table, which contains sportFK which is linked to sport table.
So in order to find out what sport the match is from, I need to do an inner join, otherwise I'd have to open the database millions of times. The only way to do it is this, but I don't know how to display the results. My poor attempt to echo the results is below:
$SQL = "SELECT sport.name,
country.name,
tournament_template.name,
tournament.name,
tournament_stage.name,
event.*
FROM tournament_template
INNER JOIN sport
ON tournament_template.sportFK = sport.id
INNER JOIN tournament ON tournament.tournament_templateFK = tournament_template.id
INNER JOIN tournament_stage ON tournament_stage.tournamentFK = tournament.id
INNER JOIN event ON event.tournament_stageFK = tournament_stage.id
INNER JOIN country ON tournament_stage.countryFK = country.id
WHERE DATE(event.startdate) = CURRENT_DATE()
ORDER BY sport.name ASC,
country.name ASC,
tournament_stage.name ASC,
event.startdate ASC";
$result = mysql_query($SQL);
while($get=mysql_fetch_array($result))
{
echo $result['event.name'];
echo "<br>";
}
Your result is fetched as an array indexed by column name, which is "name" for several of your columns... the table name sport, country, template, etc is not part of the returned index. So you need to provide column names that will be unique
Set an alias for each of your columns (e.g. SELECT sport.name AS sport_name) then reference it by its alias within your echo (e.g. $result['sport_name']).
You need to use column aliases and access those in your fetch call. Instead of event.*, be explicit about the columns you need:
$SQL = "SELECT sport.name AS sportname,
country.name AS countryname,
tournament_template.name AS templatename,
tournament.name AS tournamentname,
tournament_stage.name AS stagename,
/* Use explicit column names instead of event.* */
event.name AS eventname,
event.someothercol AS someother
FROM tournament_template
...etc...
...etc...";
// Later...
while($row=mysql_fetch_array($result))
{
echo $row['eventname'];
echo "<br>";
}
It's very confusing; it seems like I'll need to have at least one many-to-many relationship.
A track might be sung by 2+ artist - how can I store that in the database?
While showing that track, I want link to each artist so that when users click the track it opens that artist's profile page.
Can anyone provide me with some assistance in designing a class or classes to accomplish this?
Try something like that
tblSongs
SongId
Title
YearProduced
etc.
tblArtist
ArtistId
Name
ProfilePage
etc..
tblSongArtists
SongId
ArtistId
Then your queries could look something like
SELECT Title, YearProduced, Name, ProfilePage
FROM tblSongs S
LEFT OUTER JOIN tblSongArtists SA ON SA.SongId = S.SongId
LEFT OUTER JOIN tblArtists A ON A.ArtistId = SA.ArtistId
WHERE Title = 'I got the blues' -- ....
ORDER BY SongId -- or Song Title (*)
(*) order by clause is in case the search criteria produces more than one song, and if you wish to keep the artists for a given song in sequence.
Such a query produces multiple rows for a song with several artists, one row per artists, the values of the columns from tblSongs being repeated.
Any many-to-many relationship requires three tables. For your example:
Song<br/>
Name SongID
Artist <br/>
Name ArtistID
ArtistsInSongs <br/>
ArtistID SongID
I'll let you figure out the rest.
public static function find_artist_on($track_id=0) {
global $database;
$sql = "SELECT * FROM " . self::$table_name ." s " ;
$sql .= "LEFT OUTER JOIN trackartist TA ON TA.track_id =s.track_id";
$sql .= "LEFT OUTER JOIN artist A ON A.artist_id =TA.artist_id";
$sql .= " WHERE s.artist_id=" .$database->escape_value($track_id);
$sql .= " ORDER BY artist_id ASC";
return self::find_by_sql($sql);
}