Skip to content

Scintillating SQL Sentences

hanleybrand edited this page Sep 14, 2012 · 1 revision

Alphabetical list of collections

SELECT 
    data_collection.id,
    data_collection.title,
    data_collection.name,
    data_collection.owner_id,
    data_collection.hidden,
    data_collection.description,
    data_collection.agreement,
FROM
    data_collection
ORDER BY data_collection.title ASC

Getting the database id of a specific user by username

SELECT id, username
FROM `rooibos`.`auth_user`
WHERE auth_user.username = 'llux'

How many presentations does that user have?

SELECT 
    COUNT(presentation_presentation.id)
FROM
    presentation_presentation
WHERE
    presentation_presentation.owner_id = 60

getting information about records

SELECT 
    id, created, modified, name, owner_id
FROM
    data_record
WHERE
    data_record.id = 41588 

This gets some information about a record, but it's not all of the metadata, here's the output:

id created modified/td> name owner_id
41588 2007-10-04 13:32:23 2010-10-20 09:15:58 r-6165371 60

If you want detailed information, maybe it's unsurprising that some JOINs are necessary:

SELECT 
    data_record.id,
    data_record.name,
    data_fieldvalue.label,
    data_fieldvalue.value
FROM
    rooibos.data_record
        INNER JOIN
    rooibos.data_fieldvalue ON data_fieldvalue.record_id = data_record.id
WHERE
    data_record.id = 41588
41588 r-6165371 Date 1964
41588 r-6165371 Description mixed material assemblage
41588 r-6165371 Creator Kienholz, Edward
41588 r-6165371 Title Back Seat Dodge, '38