简体   繁体   中英

mysql, select data from multiple tables, without common field in tables

I have 3 tables. images are given below.

  1. news

在此处输入图片说明

  1. pages

在此处输入图片说明

  1. community

在此处输入图片说明

I want to select the data from these tables, as these tables have no common fields.

I have a search field, user enter search term, and then mysql query should search in these tables and shows the results. But i am not understanding how can i select data without common fields.

I can search in one table, but not in multiple tables.

My code

$search_term = $_REQUEST['search_term'];
$where="select * from community where 1";
if($search_term!=''){
        $where.=" and name like '%$search_term%' or zip like '%$search_term%' or city like '%$search_term%'or state like '%$search_term%'or location like '%$search_term%' or country like '%$search_term%'";
    }
$db = new Database();
$results_per_page=5;
$pg=new Paging($db,$where,$current_page,$results_per_page);
$start=$pg->get_start();
$total_pages=$pg->get_total_pages($where);
$result=$db->query("$where  order by id desc limit $start,$results_per_page");
$get_all_communitites = $result;

Any body give me some hint, how to write this mysql query to select data from multiple tables, without common field?

A result must have a defined structure. For example you can't return 20 rows, 10 of them having the columns "test", "col2", "someInt" and 10 of them having 4 columns "col1", "someName", "age", "createdDT".

If for example you have two tables (one has 3 columns, second has 4 columns) with 10 rows each and want to select all the data, you have two options:

  1. Join them. If you can't connect these tables logically, then you get all possible combinations. In my example you would have 100 rows (10*10) with 7 columns.
  2. Union. You can make two queries and pretend both are from the same imaginary table, but for that to work, both tables have to have the same number of columns.

Whatever your reasons for this may be, doing what you intend to do is doomed. Imagine MySQL-Tables like huge sheets in openCalc, Excel,... where you must define columns only once for every sheet and may only search within a single column. Doing a "Ctrl + F" for anything within any column in any table doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM