-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');
8
  • 4
    what did you try already? Commented Sep 29, 2017 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 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. Read How to Ask a good question. Be sure to take the tour and read this. Commented Sep 29, 2017 at 14:41
  • Are both databases on the same server? Commented Sep 29, 2017 at 14:41
  • 1
    Then just write the query as normal but prefix the table names with the database name. database.table.column Commented Sep 29, 2017 at 14:42
  • 1
    Please do not dump code in commments, it is unreadable. Edit your original question to add any new information. Commented Sep 29, 2017 at 14:51

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.