So i learned how to do a bulk insert using data from one table as the userId from another table. Now i tried to do the same thing but i have a SQL(ite) error. I took a guess at the syntax and i got it wrong. After i bulk insert into the subscription i want to add 1 to each of the users media count. I have a left join error. How do i correct it?
-edit- I haven't solve this yet. Please help.
NOTE: I am using sqlite ATM but I am switching to mysql or MS sql
void updateMediaForSubscribers(long userId, long mediaId, Media_Base.Catagory cat, DateTime currentDate)
{
command.CommandText =
"INSERT INTO user_media_subscription (recipientId, mediaId, catagory) " +
"SELECT watcher, #mediaId, #category " +
"FROM user_watch WHERE watched=#watched;";
command.Parameters.Add("#mediaId", DbType.Int64).Value = mediaId;
command.Parameters.Add("#category", DbType.Int64).Value = cat;
command.Parameters.Add("#watched", DbType.Int64).Value = userId;
command.ExecuteNonQuery();
//near "LEFT": syntax error
command.CommandText =
"UPDATE user_data SET mediaMsgCount=mediaMsgCount+1 " +
"LEFT JOIN user_watch AS w ON w.watcher=user_data.userId " +
"WHERE w.watched=#watched;";
command.Parameters.Add("#watched", DbType.Int64).Value = userId;
command.ExecuteNonQuery();
}
UPDATE user_data
SET mediaMsgCount=mediaMsgCount+1
LEFT JOIN user_watch AS w ON w.watcher=user_data.userId
WHERE w.watched=#watched
Looks like it won't work to me, because you're joining in an UPDATE query but without a "FROM" clause, have you tried:
UPDATE u
SET u.mediaMsgCount = u.mediaMsgCount+1
FROM user_data u
LEFT JOIN user_watch AS w ON w.watcher=u.userId
WHERE w.watched=#watched
UPDATE user_data
SET mediaMsgCount = u.mediaMsgCount + 1
FROM user_data u LEFT OUTER JOIN user_watch w ON w.watcher = u.userId
WHERE w.watched=#watched
Honestly, I'm not really sure why you're using a left outer join since the update is dependant on user_watch... might want to change the "LEFT OUTER JOIN" to an "INNER JOIN".
Also, its a good idea to prefix your table name with the owner, I.E. dbo.user_data ... if you dont do that then sql server has to do a lookup.
Related
I have the following table structure of my database:
db structure
My aim is to output every row of tbl_Therapeuten (these are my employees) with the name of their trainings (in german "Fortbildung").
The names of the trainings are stored in tbl_Forbildungen. And the trainings which each employee has is stores in tbl_Therapeut_Fortbildung. For the output I want to show the name of the training, not the id.
Like this:
output
I am programming in C#, SQL Server.
First I tried this:
string sSQL = "SELECT t.*, STRING_AGG(tf.Id_Fortbildung, ';') AS Fortbildungen " +
"FROM tbl_Therapeuten t " +
"FULL OUTER JOIN tbl_Therapeut_Fortbildung tf on t.Id = tf.Id_Therapeut ";
But this brings an error containing that I don't use tbl_Therapeuten.Id in an aggregate function or in a Group by clause.
Next I tried this:
string sSQL = "SELECT t.Id, t.Nachname, STRING_AGG(tf.Id_Fortbildung, ';') AS Fortbildungen " +
"FROM tbl_Therapeuten t " +
"FULL OUTER JOIN tbl_Therapeut_Fortbildung tf on t.Id = tf.Id_Therapeut " +
"GROUP BY t.Id, t.Nachname";
This worked (except displaying the name of the training but the Id) but I don't want to explicitly name every column of tbl_Therapeuten. I want to use "t.*". But this is not working:
string sSQL = "SELECT t.*, STRING_AGG(tf.Id_Fortbildung, ';') AS Fortbildungen " +
"FROM tbl_Therapeuten t " +
"FULL OUTER JOIN tbl_Therapeut_Fortbildung tf on t.Id = tf.Id_Therapeut " +
"GROUP BY t.*";
So I need some help :)
And what I also don't know how to do is to display the name of the trainings instead of the id.
Thanks for your help!
You might find that a correlated subquery provides better performance (by avoiding the outer aggregation) and allows you to avoid listing all columns from t:
SELECT T.*,
(SELECT STRING_AGG(F.Name, ';')
FROM tbl_Therapeut_FortBildung TF JOIN
tbl_FortBildungen F
ON F.Id = TF.Id_FortBildung
WHERE TF.Id_Therapeut = T.Id
) as Fortbildungen
FROM Therapeuten T
I think this is what you need.
SELECT T.Id,T.Name,T.Street,T.BirthDate,STRING_AGG(F.Name,';')
FROM Therapeuten T
LEFT JOIN tbl_Therapeut_FortBildung TF ON TF.Id_Therapeut=T.Id
LEFT JOIN tbl_FortBildungen F ON F.Id=TF.Id_FortBildung
GROUP BY T.Id,T.Name,T.Street,T.BirthDate
As far as I know SQL server does not support the GROUP BY *, so you will have to specify the column names.
Anyone know why got syntax error? I think there are no reserve words inside right?
C# Command
cmd.CommandText = "SELECT ordertable._name, ordertable.quantity, food_menu.food_price, beverage_menu.beverage_price" +
"FROM ordertable" +
"INNER JOIN food_menu ON ordertable._name = food.foodname" +
"INNER JOIN beverage_menu ON beverage_menu.beverage_name' = ordertable._name" +
"WHERE ordertable.tablenum = '1'";
Error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'JOIN food_menu ON ordertable._name = food.foodnameINNER JOIN
beverage_menu ON be' at line 1
You are missing spaces. At the moment your query will be like FROM ordertableINNER JOIN food_menu .... You could use the multiline syntax to make it simpler
cmd.CommandText = #"SELECT ordertable._name, ordertable.quantity, food_menu.food_price, beverage_menu.beverage_price
FROM ordertable
INNER JOIN food_menu ON ordertable._name = food_menu.foodname
INNER JOIN beverage_menu ON beverage_menu.beverage_name = ordertable._name
WHERE ordertable.tablenum = 1";
Remove unnecessary concatenation from string, you are getting an error because your query needs some spaces which you have not specified before join.
cmd.CommandText = "SELECT ordertable._name,
ordertable.quantity,
food_menu.food_price,
beverage_menu.beverage_price
FROM ordertable
INNER JOIN food_menu
ON ordertable._name = food.foodname
INNER JOIN beverage_menu
ON beverage_menu.beverage_name = ordertable._name
WHERE ordertable.tablenum = 1";
I'm trying to use a left join to set the plot.jobs row to 0 when there isn't a match found in the booking table (the check is plot.plot_id = booking.plot_id).
However I keep getting the following error message:
An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred
in MySql.Data.dll but was not handled in user code Additional
information: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'FROM plot LEFT JOIN booking ON plot.plot_id =
booking.plot_id WHERE plot.plot_id' at line 1.
I was following this template here:
UPDATE <Table2>
SET Count = 0
FROM <Table2>
LEFT JOIN <Table1>
ON <Table2>.<JoinColumn> = <Table1>.<JoinColumn>
WHERE <Table1>.<JoinColumn> IS NULL
This is my code so far:
query = "UPDATE plot SET jobs = #jobCount WHERE plot_id = #plotID AND postcode='MK';";
query += "UPDATE plot " +
"SET jobs = 0 " +
"FROM plot " +
"LEFT JOIN booking " +
"ON plot.plot_id = booking.plot_id " +
"WHERE plot.plot_id IS NULL";
cmd = new MySqlCommand(query, _connection);
You are attempting to use the SQL Server syntax for UPDATE statements on MySQL. This doesn't work.
The UPDATE-syntax of MySQL is:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
Where table_reference is defined with the JOIN-syntax.
Your query should be something like:
query += "UPDATE plot " +
"LEFT JOIN booking " +
"ON plot.plot_id = booking.plot_id " +
"SET jobs = 0 " +
"WHERE plot.plot_id IS NULL";
I am wondering if you don't mean: WHERE booking.plot_id IS NULL
Your syntax isn't quite right for MySQL. Do your LEFT JOIN before SET
Something like this should work (Note: this hasn't been tested).
query += "UPDATE plot p" +
"LEFT JOIN booking b" +
"ON p.plot_id = b.plot_id " +
"SET jobs = 0 " +
"WHERE p.plot_id IS NULL";
You are attempting to update the plot table setting jobs equal to zero when plot_id is null? What is the point of the join in this case? You are attempting to join records based on a null value which seems highly invalid, and you're not using the joined table booking for anything. I recommend removing the join and the from statement is not valid in an update as Saggio mentioned.
query = "UPDATE plot SET jobs = #jobCount WHERE plot_id = #plotID AND postcode='MK';";
query +="UPDATE plot " +
"SET jobs = 0 " +
"LEFT JOIN booking ON plot.plot_id = booking.plot_id" +
"WHERE booking.plot_id IS NULL;";
cmd = new MySqlCommand(query, _connection);
So I'm retrieving data from the database using odbc. One of my fields, which was causing the problem, is AutoNumber. In my query when I use, '" convert.toint64(empid)"', I get an error of data type missmatch. But when I used parameter, it worked perfectly. Can someone tell me what is the difference. As far as I know, AutoNumber is a long integer and long int is 64, right?
here are both of my queries:
string cmdText = "SELECT p.projID, p.projName, a.wageperday " +
"FROM ((projects p INNER JOIN assigns a ON p.projID = a.projname) " +
"INNER JOIN empos e ON a.employeeID= e.ID) " +
"WHERE a.employeeID = ?";
OdbcCommand assignslist = new OdbcCommand(cmdText, _connection);
assignslist.Parameters.AddWithValue("#empID", empid);
OdbcDataReader readassigns = assignslist.ExecuteReader();
GridView1.DataSource = readassigns;
GridView1.DataBind();
(this one works fine)
string cmdText = "SELECT p.projID, p.projName, a.wageperday " +
"FROM ((projects p INNER JOIN assigns a ON p.projID = a.projname) " +
"INNER JOIN empos e ON a.employeeID= e.ID) " +
"WHERE a.employeeID = '" + convert.toint64( empid ) + "';
OdbcCommand assignslist = new OdbcCommand(cmdText, _connection);
OdbcDataReader readassigns = assignslist.ExecuteReader();
GridView1.DataSource = readassigns;
GridView1.DataBind();
for this one, i get an error even if I removed the conversion, I get an error.
One more question, I don't understand INNER JOIN perfectly and the above code was from a user here who helped me. I don't understand why did he use "FROM ((projects p INNER JOIN assigns a ON p.projID = a.projname) " +
"INNER JOIN empos e ON a.employeeID= e.ID)"
two brackets -- I mean a bracket inside a bracket. And if I wanna join a 4th or 5th table do I have to put it inside a bracket aside from the main bracket? An example would be much appreciated!
If a.employeeID is a number then your problem are the quotes and the concatenation of a number to a string.
"WHERE a.employeeID = " + convert.toint64(empid).ToString()
but this make no sense because you have a string to build so, you could simply write
"WHERE a.employeeID = " + empid.ToString();
however use always the parameterized query. That is the correct way to go.
A parameterized query allows the Framework code to pass the parameters with the correct format and you don't have to worry about quoting values, format of dates and decimal separators. (And last but not least, you avoid any possibilities of Sql Injection)
For the second part of your question, JOIN is used to put togheter data from two tables, when you have more than one JOIN the parenthesys help to understand how the grouping from the tables are perfomed. First the data from projects and assigns are grouped together following the rules of the INNER JOIN then the resulting set of data is joined with the data from the employee table following the rules of the second join.
In your second query, instead of using convert.toint64( empid ) try using empid.ToString():
"WHERE a.employeeID = " + empid.ToString();
The error you were receiving was due to the fact that you were trying to concatenate a string with an integer.
With regards to the INNER JOINs you are using, the use of the brackets is dependant upon the database you are using. In most cases, you will not need the brackets at all and they can be removed without any issue, so you could rewrite the query to:
string cmdText = "SELECT p.projID, p.projName, a.wageperday " +
"FROM projects p " +
"INNER JOIN assigns a ON p.projID = a.projname " +
"INNER JOIN empos e ON a.employeeID = e.ID " +
"WHERE a.employeeID = " empid.ToString();
You're trying to add a string and an integer, which isn't allowed automatically. You'd have to convert the number to a string first, like this:
"WHERE a.employeeID = '" + empid.ToString() + "';
But, using parameters is the better way for other reasons (best habit to be in, to avoid SQL-injection attacks, etc.).
Here's what needs to be done:
Match 2 fields from different tables in SQL Server( done ), update fields in table1 where table1.field1 = table2.field2 (problem, it just updates all the records in the table)
Here's what I have , where tempTableName is a table imported to SQL Server for the purpose of the query:
"UPDATE Table1 SET Table1.fieldN ='" + DateTime.Now.DayOfYear + "' FROM " + tempTableName + " CROSS JOIN Table1 WHERE (" + tempTableName + ".fieldX = Table1.fieldY)"
Here's what I've figured out:
everything after the FROM is useless as far as actual functionality is concerned, it runs the query but the results are not "linked" in any way to the actual UPDATE statement
To sum up :
The query I've figured out updates all records in table1, I need the query to update only the rows matched by the query after FROM
PS. Forgive me if this seems trivial and that I haven't done research but the fact of the matter is that 2 weeks ago I'd never even heard of SQL and have relied heavily on SO for direction and advice.
Your CROSS JOIN should be an INNER JOIN instead.
"UPDATE t1
SET fieldN ='" + DateTime.Now.DayOfYear + "'
FROM " + tempTableName + " t2 INNER JOIN Table1 t1 ON t1.fieldy = t2.fieldx"