How do I use T-SQL Group By

To retrieve the number of widgets from each widget category that has more than 5 widgets, you could do this: SELECT WidgetCategory, count(*) FROM Widgets GROUP BY WidgetCategory HAVING count(*) > 5 The “having” clause is something people often forget about, instead opting to retrieve all their data to the client and iterating through it … Read more

MySQL Views – When to use & when not to

A view can be simply thought of as a SQL query stored permanently on the server. Whatever indices the query optimizes to will be used. In that sense, there is no difference between the SQL query or a view. It does not affect performance any more negatively than the actual SQL query. If anything, since … Read more

SQL – How to transpose?

MySQL doesn’t support ANSI PIVOT/UNPIVOT syntax, so that leave you to use: SELECT t.userid MAX(CASE WHEN t.fieldname=”Username” THEN t.fieldvalue ELSE NULL END) AS Username, MAX(CASE WHEN t.fieldname=”Password” THEN t.fieldvalue ELSE NULL END) AS Password, MAX(CASE WHEN t.fieldname=”Email Address” THEN t.fieldvalue ELSE NULL END) AS Email FROM TABLE t GROUP BY t.userid As you can see, … Read more

Laravel join with 3 Tables

I believe your join is wrong: $shares = DB::table(‘shares’) ->join(‘users’, ‘users.id’, ‘=’, ‘shares.user_id’) ->join(‘followers’, ‘followers.user_id’, ‘=’, ‘users.id’) ->where(‘followers.follower_id’, ‘=’, 3) ->get(); I also suggest you to name your table as follows instead, it feels a bit more natural to say user has many followers through follows and user has many followees through follows. Example $shares … Read more

INSERT XML into SQL Server 2008 database

Yes, there are issues when you try to insert XML into SQL Server 2008 and the XML contains an encoding instruction line. I typically get around using the CONVERT function which allows me to instruct SQL Server to skip those instructions – use something like this: INSERT INTO testfiles (filename, filemeta) VALUES (‘test.mp3’, CONVERT(XML, N'<?xml … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)