#sql
13 posts tagged sql. Clear filter
-
Select Basename in Postgres SQL
Postgres doesn't have a basename function per se. But it's easy to accomplish. select regexpreplace('http://www.seanbehan.com/images/logo.png', '.+/', '') ; -- Will return logo.png
-
How to Just Get SQL Statement Error with SQLAlchemy Python Database Wrapper
If you're working with SQLAlchemy, the best database driver for Python, and want to see only SQL syntax errors, you need to use the StatementError exception class. On it, is an attribute orig, that...
-
How to Log and Query SQL Queries Hitting Your Database with MySQL
Here is some code just in case you want to look at and query the queries hitting your MySQL database. Enter this from the mysql client console. mysql SET GLOBAL logoutput = 'TABLE' mysql SET GLOBAL...
-
How to Use Named Variables with Postgres and PHP PDO Driver
You can write reusable scripts with Postgres by taking advantage of named variables. A named variable starts with a : in your sql script. Here is an example select :anumber You can then use this...
-
Manual ManyToMany Through with Django's ORM
Here is a code snippet that demonstrates how to set up a ManyToMany through relationship in Django. In Rails, the equivalent would be called a hasmany through association. If you set the through...
-
How to Fix Raw query must include the primary key with Django ORM
When running raw SQL queries in Django you must include a primary key otherwise an invalid query exception is raised. Normally this is fine but when running more complex queries a primary key may...
-
Simple SQL for Counting New Signups
Here is a little snippet that will return new signups (or new records) for today select id, email, createdat::date date from signups where email not in (select distinct email from signups where...
-
How to Make Cross Database Queries with Postgres and DBLink Extension
Here are a few snippets for cross database queries. It's important to note that you must be explicit in enumerating the columns and types you are querying. Otherwise, things will probably not work...
-
Extract Domain Names From Links in Text with Postgres and a Single SQL Query
This query and pattern will return urls in text all within a single SQL query. select substring(columnname from '.://([^/])') as domainname from tablename; And here it is in a larger query, say for...
-
How to cast a string of comma separated numbers into an array of integers for Postgres
If you have an string of numbers like and you want to turn it into an array of integers you need to cast it into an integer array type. This is commonly used together when grabbing a set using the...
-
Active Record Find Methods
Active Record find methods for selecting range from http://charlesmaxwood.com/notes-from-reading-activerecordbase/
-
Managing Timestamps in MySQL with a Trigger
MySQL doesn't support having two columns with time stamping on both initialization and/or on updating at the same time. It would be nice to be able to do \this\ where the created\at column gets the...
-
Quick Syntax to Pipe an SQL Query Directly to a file
Here is a quick way to put the contents of a database table into a simple text file. This could be handy if for example, you just want to grab some emails and pop the results into a simple csv...