I wouldn&#39;t use EXISTS in this case, compare the query plans:<div><br></div><div><div>mysql&gt; explain extended select * from vtiger_account where exists (select invoiceid from vtiger_invoice where vtiger_account.accountid=vtiger_invoice.accountid);</div>
<div>+----+--------------------+----------------+------+---------------+------+---------+------+------+----------+-------------+</div><div>| id | select_type        | table          | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |</div>
<div>+----+--------------------+----------------+------+---------------+------+---------+------+------+----------+-------------+</div><div>|  1 | PRIMARY            | vtiger_account | ALL  | NULL          | NULL | NULL    | NULL | 6170 |   100.00 | Using where |</div>
<div>|  2 | DEPENDENT SUBQUERY | vtiger_invoice | ALL  | NULL          | NULL | NULL    | NULL |   10 |   100.00 | Using where |</div><div>+----+--------------------+----------------+------+---------------+------+---------+------+------+----------+-------------+</div>
<div>2 rows in set, 2 warnings (0.00 sec)</div><div><br></div><div>mysql&gt; explain extended select * from vtiger_account inner join vtiger_invoice using (accountid);</div><div>+----+-------------+----------------+--------+---------------+---------+---------+---------------------------------+------+----------+-------+</div>
<div>| id | select_type | table          | type   | possible_keys | key     | key_len | ref                             | rows | filtered | Extra |</div><div>+----+-------------+----------------+--------+---------------+---------+---------+---------------------------------+------+----------+-------+</div>
<div>|  1 | SIMPLE      | vtiger_invoice | ALL    | NULL          | NULL    | NULL    | NULL                            |   10 |   100.00 |       |</div><div>|  1 | SIMPLE      | vtiger_account | eq_ref | PRIMARY       | PRIMARY | 4       | vtiger540.vtiger_invoice.accountid |    1 |   100.00 |       |</div>
<div>+----+-------------+----------------+--------+---------------+---------+---------+---------------------------------+------+----------+-------+</div></div><div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Tue, Dec 18, 2012 at 10:39 AM, Joe Bordes <span dir="ltr">&lt;<a href="mailto:joe@tsolucio.com" target="_blank">joe@tsolucio.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
You can use DISTINCT in the columns clause and also try with EXISTS,<br>
something like:<br>
<br>
select * from vtiger_accounts where exists (select invoiceid from<br>
vtiger_invoice where vtiger_accounts.accountid=vtiger_invoice.accountid)<br>
<br>
Joe<br>
TSolucio<br>
<div><div class="h5"><br>
<br>
On 18/12/12 16:17, John Crisp wrote:<br>
&gt; Hi,<br>
&gt;<br>
&gt; I&#39;m trying to run a select statement to get a simple list of all<br>
&gt; Accounts that have been invoiced this year. MySQL is definitely not my<br>
&gt; strong point :-(<br>
&gt;<br>
&gt; I have got this far :<br>
&gt;<br>
&gt; SELECT<br>
&gt; `vtiger_account`.`accountid`,<br>
&gt; `vtiger_account`.`accountname`,<br>
&gt; `vtiger_account`.`account_no`,<br>
&gt; `vtiger_accountscf`.`cf_592`<br>
&gt;<br>
&gt; FROM `crm`.`vtiger_crmentity` INNER JOIN `crm`.`vtiger_account` INNER<br>
&gt; JOIN `crm`.`vtiger_accountscf` INNER JOIN `crm`.`vtiger_invoice`<br>
&gt;<br>
&gt; WHERE `vtiger_crmentity`.`crmid` = `vtiger_account`.`accountid`<br>
&gt; AND `vtiger_crmentity`.`crmid` = `vtiger_accountscf`.`accountid`<br>
&gt; AND `vtiger_crmentity`.`crmid` = `vtiger_invoice`.`accountid`<br>
&gt; AND `vtiger_crmentity`.`deleted` = 0<br>
&gt; AND `vtiger_invoice`.`invoicedate` &gt; 2012-01-01<br>
&gt;<br>
&gt;<br>
&gt; But if an Account has more than one Invoice, the result rows are<br>
&gt; obviously duplicated.<br>
&gt;<br>
&gt; I am pretty sure it is to do with the JOINs but I&#39;m blowed if I can<br>
&gt; figure it out.<br>
&gt;<br>
&gt; The Logic is :<br>
&gt;<br>
&gt; &quot;Show all ACCOUNTS that have been invoiced at least once this year&quot;<br>
&gt;<br>
&gt; I should get a rough list that looks like :<br>
&gt;<br>
&gt; Acct ID | Acct Name | Acct Number | Custom Field<br>
&gt;<br>
&gt; Any help would be gratefully received !!!!<br>
&gt;<br>
&gt; B. Rgds<br>
&gt; John<br>
&gt; _______________________________________________<br>
&gt; <a href="http://www.vtiger.com/" target="_blank">http://www.vtiger.com/</a><br>
<br>
</div></div>_______________________________________________<br>
<a href="http://www.vtiger.com/" target="_blank">http://www.vtiger.com/</a><br>
</blockquote></div><br></div>