Setting limit for query with UNION in Mysql

Asked

Viewed 155 times

2

I would like to know how I create a limit for a query that uses UNION in Mysql, in my case I want to define the LIMIT 5 for the following query:

$friends_a = mysql_query("(SELECT * FROM users WHERE id!='$user_q[id]' AND
id='$friends_1_q[user_one_id]') UNION ALL (SELECT * FROM users WHERE
id!='$user_q[id]' AND id='$friends_1_q[user_two_id]')");

I’ve tried with several codes I found on the internet and I can’t, which I should do?

1 answer

0

Maybe this will solve

$friends_a = mysql_query("(SELECT * FROM (SELECT * FROM users WHERE id!='$user_q[id]' AND
id='$friends_1_q[user_one_id]') UNION ALL (SELECT * FROM users WHERE
id!='$user_q[id]' AND id='$friends_1_q[user_two_id]') as tabela limit 5");
  • No, it didn’t work, it returned the empty query. What refers to 'table'?

  • table is the data resulting from UNION ALL, ie Query1 UNION ALL query2 = table, so I do a SELECT to return all table data by placing the LIMIT 5.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.