Jump to content

MySQL Views Help?


random1

Recommended Posts

I'm trying to create a 'view' in MySQL that grabs statistics on multiple tables within my database.

 

The statistics inlcude things like: "the total number of users", "total number of products" etc...

 

View's Body:

 

select 
  count(`total number of users`.`user_id`) AS `COUNT(``user_id``)` 
from `user` `total number of users`;

 

Can I do multiple SELECT statements in a view? If not how can I select data from more than 1 table in a view? Am I missing something obvious?

 

- Dean

Link to comment
Share on other sites

A view is basically just the result of a select statement.  So once your query is returning the data how you want then you just do

create view myView as (put your sql here)

 

If you want your columns to be made up of the results of different queries there are a couple was to do this.  The easiest to maintain would be something like

select q1.result as colname1, q2.result as colname2
from 
(select count(*) from some_table) q1, 
(select count(*) from some_table) q2

 

Hope that made sense.

Link to comment
Share on other sites

I tried:

 

select q1.result as colname1, q2.result as colname2
from
(select count(`product_id`) from `product`) q1,
(select count(`user_id`) from `user`) q2

 

but get the error "SQL Error (1054): Unknown column 'q1.result' in 'field list'".

Link to comment
Share on other sites

I tried:

 

SELECT q1.result AS total_users, q2.result AS total_products
FROM
(SELECT count(`user_id`) AS result FROM `user`) q1,
(SELECT count(`product_id`) AS result FROM `product`) q2;

 

and it works as a normal SQL execution but not in a view.

 

Seems like 'views' do not support subqueires.

 

Can someone please confirm this?

Link to comment
Share on other sites

"SQL Error (1349): View's SELECT contains a subquery in the FROM clause" for the code:

 

SELECT q1.result AS total_users, q2.result AS total_products
FROM
(SELECT count(`user_id`) AS result FROM `user`) q1,
(SELECT count(`product_id`) AS result FROM `product`) q2;

Link to comment
Share on other sites

I did some searching and it looks like this is a bug in mySQL that hasn't been resolved. What version are you running?  Try the query this way. I know oracle allows subqueries as columns so mySQL should as well.

SELECT (SELECT count(`user_id`) FROM `user`) AS total_users, (SELECT count(`product_id`) FROM `product`) AS total_products

 

 

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.