I would like to get only one feature from a MySQL database.

What am I currently doing?
I use the Add Vector Layer functionality in QGis to get the features (polygons) from the MySQL database. They are all displaying fine, but my problem is that I would like to get only one feature/polygon from the database, filtering by a MySQL query by example (it would be perfect).

Is there a way to do so ?

Thank you !

  • Do you need it before or after you load the layer? – Nathan W Sep 21 '12 at 10:10
  • 1
    @NathanW It doesn't matter, it would be better before because it would mean that QGIS doesn't have to load every feature, but if it's impossible, never mind :) – Julien Fouilhé Sep 21 '12 at 10:22

When you connect to your database and after you selected the table your wish to view, there is a button which says "Build Query". And then you tell qgis which features you want to fetch.

connect-window query-window


If for some reason you cannot access the build-in query builder you can create a View.

Go in the database and enter,


And then point qgis at the created view. Also check this answer for more details

  • Ah cool, it's possible ! Thanks a lot ! But I don't have that, I'm under QGIS 1.8.0-Lisboa (MAC OS). Do I have to install some plugin ? Did you click on "Add vector layer" to have this ? – Julien Fouilhé Sep 21 '12 at 11:33
  • 5
    There is no native MySQL provider for QGIS so you are not going to get a dialog like this. – Nathan W Sep 21 '12 at 12:06
  • @NathanW Why not ? – Julien Fouilhé Sep 21 '12 at 12:20
  • 2
    Because I suspect that is the MSSQL/PostGIS open dialog. – Nathan W Sep 21 '12 at 12:31
  • 1
    I understand what you need. And this answer would be fine if your database was PostGIS/MS SQL. MySQL doesn't have this dialog because QGIS uses OGR to open the database. – Nathan W Sep 21 '12 at 21:04

I found a solution using the ogr2ogr executable, which supports MySQL (if you're using PostgreSQL, see pgsql2shp and shp2pgsql). I'm using it like this and it works fine.

ogr2ogr -overwrite -where "YOUR SQL CONDITION" -f "ESRI Shapefile" SHPCREATED.shp MYSQL:vine,host=localhost,user=root YOURTABLE

If you're under Mac OS X, the executable has been installed in /Library/Frameworks/GDAL.framework/Versions/1.9/Programs/

NB: I was looking for a solution which was possible in command-line, because I'm working on a website and I would like to do these tasks automatically; therefore this is perfect for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.