Jump to content

Recommended Posts

I have been having a little play around to try to understand creating views now that I have upgraded to MySQL 5 and am stuck already (surprisingly enough!).

$query = mysql_query("
CREATE VIEW 
test 
AS
SELECT *
, DATE_FORMAT(`date`,'%W, %D %M %Y') AS showdate
,HT.team_name as HomeTeam
,VT.team_name as AwayTeam
,COMP.comp_short_name as CP
FROM
all_games
JOIN teams as HT ON (all_games.home_team = HT.team_id)
JOIN teams as VT ON (all_games.away_team = VT.team_id) 
JOIN competitions as COMP ON (all_games.comp = COMP.comp_id) 
WHERE 
$id = all_games.home_team AND all_games.home_goals IS NOT NULL 
OR
$id = all_games.away_team AND all_games.home_goals IS NOT NULL 
ORDER BY `date` DESC 
LIMIT 5");

$tpall_games = mysql_query("SELECT * FROM test");
if (!$tpall_games) {
    die(mysql_error());
};

 

The VIEW works perfectly if it is just used as a query, but I wanted to create a view so I can then run several different queries off of it.

The error returned is 'Table 'foo.test' doesn't exist' which suggests the VIEW is not being created.

 

I presume that I am missing something very simple such as actually executing the VIEW in some way before the query tries to read from it.

 

Steve

I'll skip over what I think is the source of the problem because it seems like there's another issue here.

 

Views don't accept arguments. They aren't functions. You can't create a view using some variable.

Basically, all a view does* is let you give a name to a complicated query - so you don't need to repeat a complicated query every time. When you do that then you can pretend it's a table**: SELECT from that and add your extra conditions then.

CREATE VIEW 
test 
AS
SELECT *
, DATE_FORMAT(`date`,'%W, %D %M %Y') AS showdate
,HT.team_name as HomeTeam
,VT.team_name as AwayTeam
,COMP.comp_short_name as CP
FROM
all_games
JOIN teams as HT ON (all_games.home_team = HT.team_id)
JOIN teams as VT ON (all_games.away_team = VT.team_id) 
JOIN competitions as COMP ON (all_games.comp = COMP.comp_id) 
WHERE 
all_games.home_goals IS NOT NULL 
OR
all_games.away_goals IS NOT NULL

SELECT *
FROM
test
WHERE
home_team = $id AND home_goals IS NOT NULL
OR
away_team = $id AND away_goals IS NOT NULL
ORDER BY `date` DESC
LIMIT 5

Create the view in your database - not in your script.

 

* It has other purposes too.

** For SELECTing. Not necessarily for other actions (eg, UPDATE or DELETE).

  • 2 weeks later...
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.