-2

I have two databases, database A and B which each have a table named general_table. How can I write a query that will do a cross search on both general_tables depending on Barcode Number (Each table contains Barcode Number) with connection:

$connect = mysqli_connect('localhost', 'pharmana_general', '123456', 'A');
Dan
  • 651
  • 7
  • 11
shiv zuh
  • 125
  • 11
  • 4
    what did you try already? – Jurick Pastechi Genaro Sep 29 '17 at 14:38
  • 2
    We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Sep 29 '17 at 14:41
  • Are both databases on the same server? – Patrick Q Sep 29 '17 at 14:41
  • yes same server – shiv zuh Sep 29 '17 at 14:41
  • 1
    Then just write the query as normal but prefix the table names with the database name. `database.table.column` – Patrick Q Sep 29 '17 at 14:42
  • SELECT * FROM `pharmana_Hareket_db`.`general_Table` HDB, `pharmana_urun_db`.`general_Table` UDB Where HDB.EczaneID = '101' AND HDB.Yil = '2017' AND HDB.Barkod = UDB.Barkod Group by UDB.Ana_Kategori gives me unknow field Ana_Kategori error but i have that field – shiv zuh Sep 29 '17 at 14:46
  • 1
    Please do not dump code in commments, it is unreadable. Edit your original question to add any new information. – Jay Blanchard Sep 29 '17 at 14:51
  • Possible duplicate of [How do I construct a cross database query in PHP?](https://stackoverflow.com/questions/1999235/how-do-i-construct-a-cross-database-query-in-php) – Alex Blex Sep 29 '17 at 16:39

1 Answers1

1

It sounds like, based on what you're trying, the best solution would be a union:

SELECT a.* FROM pharmana_Hareket_db.`general_Table` a 
UNION
SELECT b.* FROM pharmana_urun_db.`general_Table` b

Building on the above example, you could do:

SELECT a.* FROM pharmana_Hareket_db.`general_Table` a 
WHERE a.barCodeField = 1234567980
UNION
SELECT b.* FROM pharmana_urun_db.`general_Table` b
WHERE b.barCodeField = 1234567980

You could of course a use JOIN, depending on the data set up, but it sounds like a UNION would work for you.

Edit: I have just read that you want both, so you could try a join

SELECT * FROM pharmana_Hareket_db.`general_Table` a 
INNER JOIN pharmana_urun_db.`general_Table` b
ON a.barCodeField = b.barCodeField

...which should only return rows that have matching barcodes in both DBs

Chris J
  • 1,441
  • 9
  • 19