Naming columns from other tables - mysql

Just a quick question about naming columns that come from other tables, below i have the tables put in the SQL statement but after it I put an abbreviated version "MO" is this correct/ will this work in all situations or should i just stick to the full version like module.mod_code?
SELECT MO.MOD_CODE, MO.MOD_NAME, MO.ECTS_UNITS,MO.DESCRIPTION
FROM MODULE MO, SYLLABUS SY, PROGRAMME PR
WHERE MO.MOD_CODE = SY.MOD_CODE
AND SY.PROG_CODE = PR.PRO_CODE
AND PR.NFQ_LEVEL = ‘LEVEL 9’
AND MO.DESCRIPTION LIKE ‘%RESEARCH%’ OR DESCRIPTION LIKE ‘%QUALATIVE%’ OR DESCRIPTION LIKE ‘%QUANTITATIVE%’;
Thanks :)

If I understood correctly, you're trying to reference columns using the table alias, and are wondering if there is any difference in using MO.[column] and module.[column]?
If that is the case, it is preferred to use the table alias to reference the column. This is because you may join back to the same table to retrieve a different subset of data. If you do this, you will need to define which set you want the data to come from.
Module AS M ---- Programme AS P ------ Module AS SUBM

You cannot stick to the full version. Once you have given a table or subquery an alias, that is the name of that object in the scope of the query. Actually, what happens is that the table name becomes the table alias, so you can use it for qualifying columns in the table.
You should also learn proper explicit JOIN syntax. I am also guessing that you are missing parentheses on your WHERE clause:
SELECT MO.MOD_CODE, MO.MOD_NAME, MO.ECTS_UNITS,MO.DESCRIPTION
FROM MODULE MO JOIN
SYLLABUS SY
ON MO.MOD_CODE = SY.MOD_CODE JOIN
PROGRAMME PR
ON SY.PROG_CODE = PR.PRO_CODE
WHERE PR.NFQ_LEVEL = 'LEVEL 9' AND
(MO.DESCRIPTION LIKE '%RESEARCH%' OR
MO.DESCRIPTION LIKE '%QUALATIVE%' OR
MO.DESCRIPTION LIKE '%QUANTITATIVE%'
);
If you attempted something like SELECT MODULE.MOD_CODE in this query, it would return an error, because the table alias MODULE is not assigned to any object.

Related

MySQL Stored Procedure with Parameters for Recursive CTE

I'm working on a MySQL Way of printing an "affiliate tree" and got the thing working with Common Table Expression. I'm using the following code right now:
WITH RECURSIVE recUsers AS
(
SELECT ID, username, sponsorID, 1 AS depth, username AS path
FROM users
WHERE id = 1
UNION ALL
SELECT c.ID, c.username, c.sponsorID, sc.depth + 1, CONCAT(sc.path, ' > ', c.username)
FROM recUsers AS sc
JOIN users AS c ON sc.ID = c.sponsorID
)
SELECT * FROM recUsers;
This selects the tree underneath the user with the id 1.
Now what I'd need to get is a way to pass that id as a parameter, so I don't need to define everything from the beginning every time I want to get the result.. So my idea is to put everything in a stored prodecure and pass the id in as a parameter.. However, so far I didn't get it working and always getting various errors that are very self speaking...
Basically what I've tried was
DELIMITER //
CREATE PROCEDURE getAffiliateTree(IN userid INT())
BEGIN
---my code here, the userid 1 replaced with userid
END//
DELIMITER;
However, this doesn't seem to work.. How can I get this done?
Two things I would suggest:
Use INT, not INT(). The optional length argument to integer types is deprecated in MySQL 8.0 (which I know you're using, because you're using CTE syntax). Even if you did use the length argument, using an empty argument is not legal syntax.
Make sure that the userid input parameter name is distinct from all of the columns in the tables you reference. That is, if the table has a column named userid (any capitalization), then change the name of your input parameter. Otherwise you may make ambiguous expressions like:
... WHERE userid = userid
Even though you intend one of these to be the column and the other to be the parameter, the SQL parser has no way of knowing that. It ends up treating both as the column name, so it's trivially true on all rows of the table.
Actually, a third thing I would suggest: when you ask questions, "it doesn't seem to work" isn't clear enough. Did it produce an error? If so, what was the full error message? Did it produce no error, but didn't give you the result you wanted? If so, show a mocked-up example of what you expected, and what the query produced that didn't match. It helps to be as clear as you can when you post questions, so readers don't have to guess what trouble you need help with.

Multi part line cannot be bound

I am creating a program in VB.net and in one form.
I want to show the data of 2 tables in one Data grid view but it says that multi part cannot be found.
Here is what I have tried so far
sql = "select EntryNo.Student_Info, firstName.Student_Info,
lastName.Student_Info, gender.Student_Info, Height.First_WeightIn,
Weight.First_WeightIn, BMI.First_WeightIn, NutriStat.First_WeightIn from
Student_Info full Join First_WeightIn on EntryNo.Student_Info =
EntryNo.First_WeightIn"
As mentioned in my comments you are using the column names as the table and the table names as the columns...
It should be [TableName/Alias].[ColumnName]. Also worth mentioning Weight is a reserved keyword in SSMS, you should wrap the column in [ ]...
*Please make sure you include your schema as well, for example dbo or what ever schema you are using. For example FROM dbo.Student_Info si...
SELECT si.EntryNo,
si.firstName,
si.lastName,
si.gender,
wi.Height,
wi.[Weight], --reserved keyword in SSMS
wi.BMI,
wi.NutriStat
FROM Student_Info si
FULL JOIN First_WeightIn wi ON wi.EntryNo = si.EntryNo;

Replacing existing View but MySQL says "Table doesn't exist"

I have a table in my MySQL database, compatibility_core_rules, which essentially stores pairs of ids which represent compatibility between parts which have fields with those corresponding ids. Now, my aim is to get all possible compatibility pairs by following the transitivity of the pairs (e.g. so if the table has (1,2) and (2,4), then add the pair (1,4)). So, mathematically speaking, I'm trying to find the transitive closure of the compatibility_core_rules table.
E.g. if compatibility_core_rules contains (1,2), (2,4) and (4,9), then initially we can see that (1,2) and (2,4) gives a new pair (1,4). I then iterate over the updated pairs and find that (4,9) with the newly added (1,4) gives me (1,9). At this point, iterating again would add no more pairs.
So my approach is to create a view with the initial pairs from compatibility_core_rules, like so:
CREATE VIEW compatibility_core_rules_closure
AS
SELECT part_type_field_values_id_a,
part_type_field_values_id_b,
custom_builder_id
FROM compatibility_core_rules;
Then, in order to iteratively discover all pairs, I need to keep replacing that view with an updated version of itself that has additional pairs each time. However, I found MySQL doesn't like me referencing the view in its own definition, so I make a temporary view (with or replace, since this will be inside a loop):
CREATE OR REPLACE VIEW compatibility_core_rules_closure_temp
AS
SELECT part_type_field_values_id_a,
part_type_field_values_id_b,
custom_builder_id
FROM compatibility_core_rules_closure;
No problems here. I then reference this temporary view in the following CREATE OR REPLACE VIEW statement to update the compatibility_core_rules_closure view with one iteration's worth of additional pairs:
CREATE OR REPLACE VIEW compatibility_core_rules_closure
AS
SELECT
CASE WHEN ccr1.part_type_field_values_id_a = ccr2.part_type_field_values_id_a THEN ccr1.part_type_field_values_id_b
WHEN ccr1.part_type_field_values_id_a = ccr2.part_type_field_values_id_b THEN ccr1.part_type_field_values_id_b
END ccrA,
CASE WHEN ccr1.part_type_field_values_id_a = ccr2.part_type_field_values_id_a THEN ccr2.part_type_field_values_id_b
WHEN ccr1.part_type_field_values_id_a = ccr2.part_type_field_values_id_b THEN ccr2.part_type_field_values_id_a
END ccrB,
ccr1.custom_builder_id custom_builder_id
FROM compatibility_core_rules_closure_temp ccr1
INNER JOIN compatibility_core_rules_closure_temp ccr2
ON (
ccr1.part_type_field_values_id_a = ccr2.part_type_field_values_id_a OR
ccr1.part_type_field_values_id_a = ccr2.part_type_field_values_id_b
)
GROUP BY ccrA,
ccrB
HAVING -- ccrA and ccrB are in fact not the same
ccrA != ccrB
-- ccrA and ccrB do not belong to the same part type
AND (
SELECT ptf.part_type_id
FROM part_type_field_values ptfv
INNER JOIN part_type_fields ptf
ON ptfv.part_type_field_id = ptf.id
WHERE ptfv.id = ccrA
LIMIT 1
) !=
(
SELECT ptf.part_type_id
FROM part_type_field_values ptfv
INNER JOIN part_type_fields ptf
ON ptfv.part_type_field_id = ptf.id
WHERE ptfv.id = ccrB
LIMIT 1
)
Now this is where things go wrong. I get the following error:
#1146 - Table 'db509574872.compatibility_core_rules_closure' doesn't exist
I'm very confused by this error message. I literally just created the view/table only two statements ago. I'm sure the SELECT query itself is correct since if I try it by itself and it runs fine. If I change the first line to use compatibility_core_rules_closure2 instead of compatibility_core_rules_closure then it runs fine (however, that's not much use since I need to be re-updating the same view again and again). I've looked into the SQL SECURITY clauses but have not had any success. Also been researching online but not getting anywhere.
Does anyone have any ideas what is happening and how to solve it?
MySQL doesn't support sub-queries in views.
You'll have to separate them... ie. using another view containing the sub-query inside you main view.
Running the create statement for that view will render an error, not creating it, hence the doesn't exist error you're getting when querying it.

Why is my query wrong?

before i use alias for table i get the error:
: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
Then i used aliases and i get this error:
unknown index a
I am trying to get a list of category name ( dependant to a translation) and the associated category id which is unique. Since i need to put them in a select, i see that i should use the lists.
$categorie= DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->lists('nome.nome','a.id');
The best way to debug your query is to look at the raw query Laravel generates and trying to run this raw query in your favorite SQL tool (Navicat, MySQL cli tool...), so you can dump it to log using:
DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
});
Doing that with yours I could see at least one problem:
->where('lingua.lingua','=','it-IT')
Must be changed to
->where('lingua.lingua','=',"'it-IT'")
As #jmail said, you didn't really describe the problem very well, just what you ended up doing to get around (part of) it. However, if I read your question right you're saying that originally you did it without all the aliases you got the 'ambiguous' error.
So let me explain that first: this would happen, because there are many parts of that query that use id rather than a qualified table`.`id.
if you think about it, without aliases you query looks a bit like this: SELECT * FROM `cat` JOIN `campo_cat` ON `id_cat` = `id` JOIN `campo` ON `id` = `id_campo`; and suddenly, MySQL doesn't know to which table all these id columns refer. So to get around that all you need to do is namespace your fields (i.e. use ... JOIN `campo` ON `campo`.`id` = `campo_cat`.`id_campo`...). In your case you've gone one step further and aliased your tables. This certianly makes the query a little simpler, though you don't need to actually do it.
So on to your next issue - this will be a Laravel error. And presumably happening because your key column from lists($valueColumn, $keyColumn) isn't found in the results. This is because you're referring to the cat.id column (okay in your aliased case a.id) in part of the code that's no longer in MySQL - the lists() method is actually run in PHP after Laravel gets the results from the database. As such, there's no such column called a.id. It's likely it'll be called id, but because you don't request it specifically, you may find that the ambiguous issue is back. My suggestion would be to select it specifically and alias the column. Try something like the below:
$categories = DB::table('cat as a')
->join('campo_cat as c','c.id_cat','=','a.id')
->join('campo as d','d.id','=','c.id_campo')
->join('cat_nome as nome','nome.id_cat','=','a.id')
->join('lingua','nome.id_lingua','=','lingua.id')
->where('lingua.lingua','=','it-IT')
->groupby('nome.nome')
->select('nome.nome as nome_nome','a.id as a_id') // here we alias `.id as a_id
->lists('nome_nome','a_id'); // here we refer to the actual columns
It may not work perfectly (I don't use ->select() so don't know whether you pass an array or multiple parameters, also you may need DB::raw() wrapping each one in order to do the aliasing) but hopefully you get my meaning and can get it working.

MySQL WHERE clause not working after CSV import

I have a problem with a MySQL WHERE clause. I think I know what the problem is, just not how to fix it.
I have a database with student timetable information and I'm matching this against a table with student information. The student information has been imported into the database from a CSV (utf-8) file, the other information was just inserted into the database with "normal" INSERT queries.
The WHERE clause is simple and looks like this:
WHERE gpu_timetable.cls_name =
(SELECT cls_name FROM gpu_students WHERE std_number = 123441 LIMIT 1)
Its matching the cls_name (class name) from the timetable against the class name from the students table. Like I said the data is from different sources but looks to be the same. For example when I remove the SELECT query and use this string ('LV6A') the code works.
The collation on both of the fields is *utf8_general_ci*, I also tried TRIM() but no success, the same for replacing the operator = with LIKE.
Did I do something wrong when importing the student information or is there another function similar to TRIM() that can fix this weird problem?
Your simplified query must be:
SELECT * FROM gpu_timetable INNER JOIN gpu_students ON gpu_timetable.cls_name = gpu_students.cls_name WHERE gpu_students.std_number = 123441
Your should always have tablename.fieldname while using JOIN queries.