I've created a View with a SQL query:
CREATE VIEW rightview
AS
SELECT data.accounts.login,data.accounts.password,data.customers.right_
FROM data.accounts join data.customers using (id);
When i cliick on execute the query, it indicates that the View was created successfully. But when I try to display its columns with this query
select * from data.rightview;
I got this error:
Error code 1356, SQL state HY000: View 'data.rightview' references
invalid table(s) or column(s) or function(s) or definer/invoker of
view lack rights to use them
IMHO better will be:
CREATE VIEW rightview
AS
SELECT acc.login, acc.password, cust.right_
FROM data.accounts acc
LEFT JOIN data.customers cust on acc.id=cust.id
if table customers include column ID which is reference to table accounts...
Try using table alias
CREATE VIEW rightview
AS
SELECT account.login,account.password,customer.right_
FROM data.accounts as account join data.customers as customer using (id);
and
select * from rightview;
Try this one..
CREATE VIEW rightview
AS SELECT a.login,a.password,b.right_
FROM accounts a INNER JOIN customers b
on a.id = b.id;
then,
select * from rightview;
will work..
I got same error. I solved coping all code from original view in the original db and pasted into bkp_db creating other view...
Related
I can successfully join two tables, but i'm trying to output the results of the join into a new table.
The following throws a syntax error, but works if the "into" line is omitted.
To be clear - not having problems w/ the join, but the "into" statement.
SELECT evictions.uniqueid_neighborhoods.id, evictions.uniqueid_neighborhoods.neighbor_1,
evictions.sanfrancisco_evictions_backup2.Breach,
evictions.sanfrancisco_evictions_backup2.NonPayment,
evictions.sanfrancisco_evictions_backup2.Nuisance,
evictions.sanfrancisco_evictions_backup2.IllegalUse
** This causes code to fail
into evictions_by_commArea
from evictions.uniqueid_neighborhoods
inner join evictions.sanfrancisco_evictions_backup2 on evictions.uniqueid_neighborhoods.id = evictions.sanfrancisco_evictions_backup2.id
MySQL uses CREATE TABLE AS:
CREATE TABLE evictions_by_commArea as
SELECT un.id, un.neighbor_1,
sfe.Breach,
sfe.NonPayment,
sfe.Nuisance,
sfe.IllegalUse
from evictions.uniqueid_neighborhoods un join
evictions.sanfrancisco_evictions_backup2 sfe
on un.id = sfe.id;
I also introduced able aliases so the query is easier to write and to read.
I get a MySQL Error saying, I cannot use more than 61 tables in a join. I need to avoid this error. How do I do it? Please Help.
select
view_pdg_institutes.user_id as User_ID,
view_pdg_institutes.institute_id as Teacher_ID,
view_pdg_institutes.institute_name as Institute_Name,
view_pdg_institutes.user_email as Email,
view_pdg_institutes.contact_person_name as Contact_Person,
view_pdg_institutes.alternative_contact_no as Alternative_Mobile_No,
view_pdg_institutes.primary_contact_no as Mobile_No,
view_pdg_institutes.correspondance_address as Address,
view_pdg_institutes.other_communication_mode as Preferred_Contact_Mode,
view_pdg_institutes.size_of_faculty as Size_of_Faculty,
view_pdg_institutes.operation_hours_from as Operation_Hours_From,
view_pdg_institutes.operation_hours_to as Operation_Hours_To,
view_pdg_institutes.teaching_xp as Teaching_Experience,
view_pdg_institutes.installment_allowed as Installment_Allowed,
view_pdg_institutes.about_fees_structure as About_Fees_Structure,
view_pdg_institutes.no_of_demo_class as No_of_Demo_Classes,
view_pdg_institutes.demo_allowed as Demo_Allowed,
view_pdg_institutes.price_per_demo_class as Price_Per_Demo_Class,
view_pdg_tuition_batch.tuition_batch_id as Batch_ID,
view_pdg_batch_subject.subject_name as Subject_Name,
view_pdg_batch_subject.subject_type as Subject_Type,
view_pdg_batch_subject.academic_board as Academic_Board,
view_pdg_batch_fees.fees_type as Fees_Type,
view_pdg_batch_fees.fees_amount as Fees_Amount,
view_pdg_tuition_batch.course_days as Course_Days,
view_pdg_tuition_batch.days_per_week as Days_Per_Week,
view_pdg_tuition_batch.class_duration as Class_Duration,
view_pdg_tuition_batch.class_type as Class_Type,
view_pdg_tuition_batch.course_length as Course_Length,
view_pdg_tuition_batch.course_length_type as Course_Length_Type,
view_pdg_tuition_batch.no_of_locations as No_of_Locations,
view_pdg_tuition_batch.class_capacity_id as Class_Capacity_ID,
view_pdg_tutor_location.locality as Locality,
view_pdg_tutor_location.address as Address,
view_pdg_batch_class_timing.class_timing as Class_Timing
from view_pdg_tuition_batch
left join view_pdg_institutes on (view_pdg_tuition_batch.tutor_institute_user_id = view_pdg_institutes.user_id)
left join view_pdg_batch_subject on (view_pdg_batch_subject.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_batch_fees on (view_pdg_batch_fees.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_batch_class_timing on (view_pdg_batch_class_timing.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
left join view_pdg_tutor_location on (view_pdg_tutor_location.tuition_batch_id = view_pdg_tuition_batch.tuition_batch_id)
group by view_pdg_tuition_batch.tuition_batch_id;
I need a solution that would not require changing the current approach of writing the query.
I don't think it's possible to do what you're asking without some elaborate changes in the way you store and query data. You can
denormalize your DB to store JSON data;
create materialized views, emulating them via triggers, because they're absent in MySQL;
use temporary tables;
join partial selects by hand at the call site;
compile MySQL with another join limit;
use proper SQL engine like Postgres, that doesn't suffer from such stupid things.
Insert the contents of each view into its own temporary table. Then do the same query with the temporary table names substituted for the original view names.
In MySQL, I have defined a view on two tables as follows:
delimiter $$
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost`<br/>
SQL SECURITY DEFINER VIEW `test`.`viewinschrijvingen` AS<br/>
select `i`.`student` AS `student`,<br/>
`i`.`opleidingscode` AS `opleidingscode`,<br/>
`i`.`inschrijvingsvorm` AS `inschrijvingsvorm`,<br/>
`i`.`brin` AS `brin`,<br/>
`i`.`brinvolgnummer` AS `brinvolgnummer`,<br/>
`o`.`onderwijsvorm` AS `onderwijsvorm`,<br/>
`o`.`opleidingniveau` AS `opleidingniveau`,<br/>
`o`.`naamopleidingkort` AS `naamopleidingkort`,<br/>
`o`.`instelling` AS `instelling`,<br/>
`o`.`studielast` AS `studielast`,<br/>
date_format(max(str_to_date(`i`.`datuminschrijving`,'%Y-%m-%d')),'%Y-%m-%d') AS `datuminschrijving`,<br/>
`o`.`gemeentenaam` AS `gemeentenaam` from<br/>
(`test`.`inschrijvingen` `i` left outer join `test`.`opleidingen` `o`<br/>
on((`i`.`opleidingscode` = `o`.`opleidingscode`)))<br/>
group by `i`.`opleidingscode`,`i`.`brin`,`i`.`brinvolgnummer`$$<br/>
When I query this view for the information on a specific student:
SELECT * FROM test.viewinschrijvingen WHERE student = '310018717'
the result is empty (no records returned). When I browse through the records in the view, there is no record for student 310018717 (obviously).
However, when I execute the query I used to create the view directly:
select `i`.`student` AS `student`,<br/>
`i`.`opleidingscode` AS `opleidingscode`,<br/>
`i`.`inschrijvingsvorm` AS `inschrijvingsvorm`,<br/>
`i`.`brin` AS `brin`,<br/>
`i`.`brinvolgnummer` AS `brinvolgnummer`,<br/>
`o`.`onderwijsvorm` AS `onderwijsvorm`,<br/>
`o`.`opleidingniveau` AS `opleidingniveau`,<br/>
`o`.`naamopleidingkort` AS `naamopleidingkort`,<br/>
`o`.`instelling` AS `instelling`,<br/>
`o`.`studielast` AS `studielast`,<br/>
date_format(max(str_to_date(`i`.`datuminschrijving`,'%Y-%m-%d')),'%Y-%m-%d') AS `datuminschrijving`,<br/>
`o`.`gemeentenaam` AS `gemeentenaam` from<br/>
(`test`.`inschrijvingen` `i` left outer join `test`.`opleidingen` `o`
on((`i`.`opleidingscode` = `o`.`opleidingscode`)))<br/>
WHERE student = '310018717'<br/>
group by `i`.`opleidingscode`,`i`.`brin`,`i`.`brinvolgnummer`<br/>
I do get a result (1 record, which is the result I expected). Can anybody help me to find what is causing this behaviour?
It probably has to do with your use of MySQL's GROUP BY extension versus the ANSI GROUP BY format. MySQL does not require you to group on every column that is not an aggregate function. For columns that you are not GROUPing on, MySQL can choose whatever value it wants for the column. In your case, you are not using the student field to group and thus it may not be choosing the value you are searching for.
You may want to try this query which uses the ANSI GROUP BY and see if you get the results you want.
delimiter $$
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost`
SQL SECURITY DEFINER VIEW `test`.`viewinschrijvingen` AS
select
`i`.`student` AS `student`,
`i`.`opleidingscode` AS `opleidingscode`,
`i`.`inschrijvingsvorm` AS `inschrijvingsvorm`,
`i`.`brin` AS `brin`,
`i`.`brinvolgnummer` AS `brinvolgnummer`,
`o`.`onderwijsvorm` AS `onderwijsvorm`,
`o`.`opleidingniveau` AS `opleidingniveau`,
`o`.`naamopleidingkort` AS `naamopleidingkort`,
`o`.`instelling` AS `instelling`,
`o`.`studielast` AS `studielast`,
date_format(max(str_to_date(`i`.`datuminschrijving`,'%Y-%m-%d')),'%Y-%m-%d') AS `datuminschrijving`,
`o`.`gemeentenaam` AS `gemeentenaam`
from `test`.`inschrijvingen` `i`
left outer join `test`.`opleidingen` `o`
on `i`.`opleidingscode` = `o`.`opleidingscode`
group by
`i`.`student`,
`i`.`opleidingscode`,
`i`.`inschrijvingsvorm`,
`i`.`brin` AS `brin`,
`i`.`brinvolgnummer`,
`o`.`onderwijsvorm`,
`o`.`opleidingniveau`,
`o`.`naamopleidingkort`,
`o`.`instelling`,
`o`.`studielast`,
`o`.`gemeentenaam`$$
I'm relatively new to Hibernate. I have to use the 3.2 version and i need to use DetachedCriteria and obtain the following query:
select this_.ID as ID0_1_, this_.SNDG as SNDG0_1_
, this_.NDG as NDG0_1_, this_.T_GWR_PARTNER_ID as T4_0_1_
, table2x1_.ID as ID1_0_, table2x1_.T_GWR_PROPOSAL_ID as T2_1_0_
, table2x1_.GROUP_SNDG as GROUP3_1_0_, table2x1_.GROUP_NAME as GROUP4_1_0_
from t_gwr_proposals this_
inner join
t_gwr_proposal_ratings table2x1_
where table2x1_.T_GWR_PROPOSAL_ID=this_.ID
but I obtain the follwing
select this_.ID as ID0_1_, this_.SNDG as SNDG0_1_
, this_.NDG as NDG0_1_, this_.T_GWR_PARTNER_ID as T4_0_1_
, table2x1_.ID as ID1_0_, table2x1_.T_GWR_PROPOSAL_ID as T2_1_0_
, table2x1_.GROUP_SNDG as GROUP3_1_0_, table2x1_.GROUP_NAME as GROUP4_1_0_
from t_gwr_proposals this_
inner join t_gwr_proposal_ratings table2x1_
** on this_.ID=table2x1_.ID **
where table2x1_.T_GWR_PROPOSAL_ID=this_.ID
using this code:
Criteria c = session.createCriteria(T_gwr_proposals.class, "Table1");
c.createAlias("Table1.T_gwr_proposal_ratings", "Table2"); // inner join by default
c.add(Restrictions.eqProperty("Table2.t_gwr_proposal_id", "Table1.proposalsId"));
return c.list();
Can anyone help me, please?
Thank you very much,
Tommaso A.
Criteria doesn't work with tables, but with entities and their association. You can only join two entities via the associations that exist between them. And only one root entity can exist in a criteria query. So, you won't be able to create such a query in Criteria (HQL should do fine, though) unless an association exists between the entities that uses table2x1_.T_GWR_PROPOSAL_ID=this_.ID as its mapping.
I have a function "fnc_FindIssueId" which accepts an object id and return its assigned issue Id.
When I call the function using pure select statements, it works fine:
select fnc_FindIssueId(150083); // returns 1 as issueId for objectId of 150083
select fnc_FindIssueId(150072); // returns 2 as issueId for objectId of 150072
But when I use it within an Inner Join, it goes into a never-ending loop:
select so.id, si.id
from smart_objects as so
LEFT OUTER join smart_issues as si
on si.id = fnc_FindIssueId(so.id)
where so.id in (150083, 150072);
What's the reason and how to resolve it?
It does not perform never-ending loop.
The reason for that is because the server performs FULL TABLE SCAN which is very slow. This condition si.id = fnc_FindIssueId(so.id) doesn't use an index even if you have define one on si.id and so.id.
The best ways you can do are:
to alter the table smart_objects
another column for the assigned issue Id
define an index on the new column
The workaround was to create a new view with ObjectId and IssueId columns then calling that function from within that view! but it has become very slow now.
CREATE ALGORITHM=UNDEFINED DEFINER=`mysql`#`%` SQL SECURITY DEFINER VIEW `vw_smart_objectissue` AS select `so`.`id` AS `objectid`,`fnc_FindIssueId`(`so`.`id`) AS `issueid` from `smart_objects` `so` order by `so`.`id`$$