Export specific rows from a PostgreSQL table as INSERT SQL script

Create a table with the set you want to export and then use the command line utility pg_dump to export to a file: create table export_table as select id, name, city from nyummy.cimory where city = ‘tokyo’ $ pg_dump –table=export_table –data-only –column-inserts my_database > data.sql –column-inserts will dump as insert commands with column names. –data-only … Read more

Condition within JOIN or WHERE

The relational algebra allows interchangeability of the predicates in the WHERE clause and the INNER JOIN, so even INNER JOIN queries with WHERE clauses can have the predicates rearrranged by the optimizer so that they may already be excluded during the JOIN process. I recommend you write the queries in the most readable way possible. … Read more

COUNT(*) vs. COUNT(1) vs. COUNT(pk): which is better? [duplicate]

Bottom Line Use either COUNT(field) or COUNT(*), and stick with it consistently, and if your database allows COUNT(tableHere) or COUNT(tableHere.*), use that. In short, don’t use COUNT(1) for anything. It’s a one-trick pony, which rarely does what you want, and in those rare cases is equivalent to count(*) Use count(*) for counting Use * for … Read more

Fastest way to count exact number of rows in a very large table?

Simple answer: Database vendor independent solution = use the standard = COUNT(*) There are approximate SQL Server solutions but don’t use COUNT(*) = out of scope Notes: COUNT(1) = COUNT(*) = COUNT(PrimaryKey) just in case Edit: SQL Server example (1.4 billion rows, 12 columns) SELECT COUNT(*) FROM MyBigtable WITH (NOLOCK) — NOLOCK here is for … Read more

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