  We determine the mean number of 3-torsion elements in the class groups of
quadratic orders, where the quadratic orders are ordered by their absolute
discriminants. Moreover, for a quadratic order $\mathcal{O}$ we distinguish
between the two groups: $\mathrm{Cl}_3(\mathcal{O})$, the group of ideal
classes of order $3$; and $\mathcal{I}_3(\mathcal{O})$, the group of ideals of
order $3$. We determine the mean values of both $|\mathrm{Cl}_3(\mathcal{O})|$
and $|\mathcal{I}_3(\mathcal{O})|$, as $\mathcal{O}$ ranges over any family of
orders defined by finitely many (or in suitable cases, even infinitely many)
local conditions.
  As a consequence, we prove the surprising fact that the mean value of the
difference $|\mathrm{Cl}_3(\mathcal{O})|-|\mathcal{I}_3(\mathcal{O})|$ is equal
to $1$, regardless of whether one averages over the maximal orders in complex
quadratic fields or over all orders in such fields or, indeed, over any family
of complex quadratic orders defined by local conditions. For any family of real
quadratic orders defined by local conditions, we prove similarly that the mean
value of the difference
$|\mathrm{Cl}_3(\mathcal{O})|-\frac13|\mathcal{I}_3(\mathcal{O})|$ is always
equal to $1$, independent of the family.
