This paper investigates multi join queries in ad hoc wireless networks. A two-phase query optimization method is presented, which optimizes a query with two phases. First, it optimizes middle result of a query and produces optimal join sequence, which makes account of transmission data minimum. Second, the join tree from first phase is separated into several sub queries and distributed to the corresponding data nodes. The data nodes dynamically select nodes to execute these sub queries. Also, this paper presents optimization algorithm of middle result, query separation algorithm, query execution nodes selection algorithm and query plan execution algorithm. The experiment results show that the algorithms not only greatly decrease energy cost of a query but also adapt to characteristics of ad hoc wireless networks, including multi hop communication and dynamically changing topology.