Searching multiple database table with a single search parameter - php

I have four tables in a database now asking how can I search the four tables using a single search parameter with PHPMYSQL and display the result in a single page?
thanks alot friends.

Select *
From table1 inner join table2 on table1.col=table2.col
inner join table3 on table#.col=table3.col ..........
where Col=?
you can replace on ?=? with "using (column)" if both tables have the same column name.

Related

Combining two MySQL tables yet want one column to equal another

I am trying to join two tables together in a view. My first table combines two tables but now I am trying to join that table to a second table. I am trying to use a join to make sure the second table which contains roles which are performed by the user, matches up with the first table but if there are no records in the second table, I still want all the records from table 1. I know my explanation is a little confused, so my code is as follows:
CREATE VIEW `data` AS SELECT
`information`.`username`, `information`.`department`,
`information`.`company`, `information`.`title`,
`information`.`internal_number` AS `user_internal_phone`,
`information`.`external_number`AS `user_external_phone`,
`information`.`mail`, `information`.`cn` AS `name`, `information`.`hours`,
`information`.`languages`, `information`.`functions`,
`information`.`service` AS `contact_point_name_one`,
`information`.`subservice` AS `contact_point_name_two`,
`information`.`internal_phone` AS `contact_internal_phone`,
`information`.`external_phone` AS `contact_external_phone`,
`information`.`keywords`, `information`.`description`,
`information`.`provided_by`, `information`.`id`,
GROUP_CONCAT(`staff_roles`.`role` SEPARATOR ', ') AS `roles`,
`staff_roles`.`user`
FROM `information`
CROSS JOIN `staff_roles` ON `information`.`username` = `staff_roles`.`user`;
I get an error when I do an outer join and a cross join and an inner join both return rows where there is a row in both tables yet I want it to display the rows where there is nothing in the second table as well. The purpose of using a join is so that, where there is a match, the row from table 2 should match the row on table 1
Use LEFT JOIN. The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
Just replace your CROSS JOIN with LEFT JOIN
Simply use LEFT JOIN instead of CROSS JOIN:
CREATE VIEW `data` AS SELECT
...
FROM `information`
LEFT JOIN `staff_roles` ON `information`.`username` = `staff_roles`.`user`;
Take a look at http://www.w3schools.com/sql/sql_join_left.asp for further details about why LEFT JOIN.

MySQL Join Query with Multiple Joins

I have three different tables I am trying to pull info from. Related in different ways. I am trying to find a Join statement that will allow me to pull the information a little better than using the two queries I am now.
Here is what I have
SELECT * FROM reports LEFT JOIN students USING (sID)
This allows me to properly merge the two tables. However - There is a row in the reports table named pID. I have another table named test, that also has a matching pID row, along with other data. I am trying to access the data from that table as well. Is it possible to join three tables in that way? Not only getting data from the sID, but the pID as well
You can do the following
SELECT *
FROM reports
LEFT JOIN students USING (sID)
LEFT JOIN table3 USING (pID)
Where table3 is the name of your table.
Hi robert yes it is posible. You can do a query like as shown below given your three tables, reports, students, and test.
SELECT *
FROM `reports`
LEFT JOIN `students` ON `students.sid=reports.sid`
LEFT JOIN `test` ON `reports.pid=tests.pid`

Getting two values from same Joined table

I have two tables:
procedures
countries
Table procedures has two columns
destination_country_iso
origin_country_iso
Table countries
iso
name
Im trying to join up the two tables in order to get both destination an origin country name from countries table.
SELECT *
FROM draft_procedures AS drp
LEFT JOIN countries AS c1 ON drp.destination_country_iso = c1.iso
LEFT JOIN countries AS c2 ON drp.origin_country_iso = c2.iso
The SQL result display proper result, but I now have two duplicate columns "name" and Im unable to retrieve them with the php code:
$destination_country=$row['c1.name'];
access it like this
SELECT drp.*,c1.name as c1name,c2.name as c2name
then
$row['c1name'] $row['c2name']

mysql query result with join/concat in php codeigniter

I have two tables, one for students and one for subjects, and I want to join these two tables, so that each student list will contain all subjects belonging to them.
Can this be done in single select query alone?
Or should I query for each table and play with array results?
Table example:
You can use group_concat to get the first option of the desired output:
SELECT student_id, name, GROUP_CONCAT(subject SEPARATOR ' ')
FROM table1
JOIN table2 ON table1.student_id = table2.student_no
GROUP BY student_id, name

php and mysql queries

I'm creating a website where the user can add some information about multiple computers into a database.
In the mysql database I have 3 tables. TypeOfMachine, Software and Hardware. The only common thing they have is the NumberOfMachine.
I must create a page where the user can run reports showing devices that have a specific piece of software installed (user specified) or specific hardware (user specified) after he submitted the computer's info.
Any ideas how I can do this?
And how I can connect all 3 tables into one?
I thought about this code but i dont know what to put in WHERE. I have 10 variables. and I have to show all the computers with what the user has asked and their info as well!
$search1 = "
SELECT
TypeOfMachine.NumberOfMachine, TypeOfMachine.TypeOfMachine, TypeOfMachine.OS, Software.Software1, Software.Software2, Software.Software3, Hardware.SSD, Hardware.Harddisk, Hardware.MonitorSize, Hardware.Ram, Hardware.Rom, Hardware.Processor
FROM TypeOfMachine, Software, Hardware
WHERE
but i
You want to use a join. This example is based on the fact that you've said the NumberOfMachine field is present in all tables and is a common link between them:
SELECT
TypeOfMachine.NumberOfMachine,
TypeOfMachine.TypeOfMachine,
TypeOfMachine.OS,
Software.Software1,
Software.Software2,
Software.Software3,
Hardware.SSD,
Hardware.Harddisk,
Hardware.MonitorSize,
Hardware.Ram,
Hardware.Rom,
Hardware.Processor
FROM TypeOfMachine
LEFT JOIN Software
ON Software.NumberOfMachine = TypeOfMachine.NumberOfMachine
LEFT JOIN Hardware
ON Hardware.NumberOfMachine = TypeOfMachine.NumberOfMachine
WHERE
...
It's general question, I don't know which tables contains a spesific columns as indicator for all tables. It's about inner and outer join:
The two common types of joins are an inner join and an outer join. The difference between an inner and outer join is in the number of rows included in the results table.
Inner join: The results table produced by an inner join contains only rows that existed in both tables.
Outer join: The combined table produced by an outer join contains all rows that existed in one table with blanks in the columns for the rows that did not exist in the second table.
For instance, if table1 contains a row for Joe and a row for Sally, and table2 contains only a row for Sally, an inner join would contain only one row: the row for Sally. However, an outer join would contain two rows — a row for Joe and a row for Sally — even though the row for Joe would have a blank field for weight.
The results table for the outer join contains all the rows for one table. If any of the rows for that table don’t exist in the second table, the columns for the second table are empty. Clearly, the contents of the results table are determined by which table contributes all its rows, requiring the second table to match it.
Two kinds of outer joins control which table sets the rows and which must match: a LEFT JOIN and a RIGHT JOIN.
You use different SELECT queries for an inner join and the two types of outer joins. The following query is an inner join:
SELECT columnnamelist FROM table1,table2
WHERE table1.col2 = table2.col2
And these queries are outer joins:
SELECT columnnamelist FROM table1 LEFT JOIN table2
ON table1.col1=table2.col2
SELECT columnnamelist FROM table1 RIGHT JOIN table2
ON table1.col1=table2.col2
In all three queries, table1 and table2 are the tables to be joined. You can join more than two tables. In both queries, col1 and col2 are the names of the columns being matched to join the tables. The tables are matched based on the data in these columns. These two columns can have the same name or different names, but they must contain the same type of data.
For general concept you can use #Scrowler suggestion, or this one:
http://stackoverflow.com/questions/1204217/mysql-select-join-3-tables

Categories