Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Mysql select statement
 
MathieuF
Sr. Member
 
Total Posts:  141
Joined:  2009-03-12
Montreal, Canada
 

This might sounds like a stupid question but I would like to know how to write a mysql query in magento.

I’ve got this:

$conn $this->getConnection();
$this->_select $conn->select()->from(array('main_table' => $this->getResource()->getMainTable()), array('*')
         ->
where('`date_from` >= ?',$date_start)
         ->
where('`date_to` <= ?',$date_stop)
         ->
order('event_id');
return 
$this;

but it doesn’t lead me anywhere. Not sure I understand it fully.

Basically I have 2 dates, I want to query everything in between the two dates.

Thansk for your help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  3217
Joined:  2009-02-26
Minneapolis MN
 

Here is an example to get your started

$select null;
            
            
$eavAttributeOption $resource->getTableName('eav/attribute_option');
            
$eavAttributeOptionValue $resource->getTableName('eav/attribute_option_value');
    
            
            
$select $read->select()->from(array('eaov'=>$eavAttributeOptionValue))
                ->
join(array('eao'=>$eavAttributeOption), 'eaov.option_id = eao.option_id', array())
                ->
where('eao.attribute_id=?'$pro);
                        
            
$optionIds $read->fetchAll($select);
            
            foreach (
$optionIds as $row)
                
{
                    $ops[] 
$row['value'].' '.$this->getOptionValue($row['option_id']);
                
                
}
            
            
return $ops;
 
Magento Community Magento Community
Magento Community
Magento Community
 
FetchDesigns
Sr. Member
 
Avatar
Total Posts:  124
Joined:  2008-04-11
Chicago, IL
 

I am looking to do this as well only need to run a select statement off of a new custom table currently named ppd_rescues.  How would I modify this to pull the information.  Currently I am getting this error… Fatal error: Call to a member function getTableName() on a non-object in /home/fetchdes/public_html/ppd_magento_dev/app/code/local/Mayerwin/Custom/Block/Test.php on line 68

Any ideas?  Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  3217
Joined:  2009-02-26
Minneapolis MN
 

$readonce Mage::getSingleton('core/resource')->getConnection('core_read');
$rows $readonce->fetchAll('SELECT * FROM catalog_product_entity_int where attribute_id ='.$attribId);
        
        foreach (
$rows as $row)
            
{
            $ops[] 
= array($row['entity_id']$this->getOptionLabel($row['value']).' '.$this->getOptionValue($row['value']));                                
            
}
        
        
        
return $ops;

The is an example of pulling from a table, you can just replace the names

 
Magento Community Magento Community
Magento Community
Magento Community
 
FetchDesigns
Sr. Member
 
Avatar
Total Posts:  124
Joined:  2008-04-11
Chicago, IL
 

Thanks for your fast response!  What would I need to change in this?  Obviously the select statement and the row names, but what does the “core/resource” and “core_read” do?  Should I keep those as is?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brent W Peterson
Moderator
 
Avatar
Total Posts:  3217
Joined:  2009-02-26
Minneapolis MN
 

You need everything in the second option as it sits

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top