front end web guy thrown into the SQL abyss. Below is a prototype of our current SQL query:
INSERT INTO table (table column)
SELECT ad.val1, web.val2, ad.val3
FROM ad
LEFT JOIN web
ON ad.val 4 = web.val4
We've recently added a web.val5 column and we want to insert it's information to the same column as web.val4
I tried the following using the ALL clause:
INSERT INTO table (table column)
SELECT ad.val1, web.val2, ad.val3
FROM ad
LEFT JOIN web
ON ad.val 4 = web.val4
AND ad.val4 = web.val5
But I keep getting 0 (or NULL?). Is there a certain clause that I can add to a LEFT JOIN that can equate a value to two different values in different columns within the same table? Sorry if this is confusing, I barely understand it myself.
You can consider joining it twice and match that condition like
INSERT INTO table (table column)
SELECT ad.val1, web.val2, ad.val3
FROM ad
LEFT JOIN web
ON ad.val4 = web.val4
LEFT JOIN web w //second join
ON ad.val4 = w.val5
Guessing a little bit here, but if I understand your issue, your and criteria is making it return the null values. Instead you want or or in:
INSERT INTO table (table column)
SELECT ad.val1, web.val2, ad.val3
FROM ad
LEFT JOIN web ON ad.val4 IN (web.val4, web.val5)
Related
I'm trying to create an sql query that will show me the prices of the services selected by one user within a reservation.
So I want to select the id_service column from my table chosen_services where the id_reservation coresponds to the $_SESSION['idReservation'] and get the price of these services and finaly calculate the total.
here are my 2 tables in mysql : https://ibb.co/NtgLLh7 and https://ibb.co/jkB0Qm6
Here's my query for the moment :
$getPriceOfAllServices = $bdd->prepare(
'SELECT id_service
FROM CHOSEN_SERVICES
LEFT JOIN SERVICES ON CHOSEN_SERVICES.id_chosen_service = SERVICES.id_service
WHERE id_reservation = ?'
);
$getPriceOfAllServices->execute(array($_SESSION['idReservation']));
$getPriceOfAllServices = $getPriceOfAllServices->fetch();
And I have this error : https://ibb.co/Wc1hLvz
Thanks in advance!
SELECT id_service
Both tables involved in the query have a column called id_service. Since you did not prefix the column name, MySQL cannot reliably tell which of the two you mean, hence it raises the 'ambiguous column name` error.
Since you are using LEFT JOIN, it is very likely that you want to return the column coming from table CHOSEN_SERVICES (since the other column might be NULL, when the join condition does not match).
Consider:
SELECT CHOSEN_SERVICES.id_service
FROM CHOSEN_SERVICES
LEFT JOIN SERVICES ON CHOSEN_SERVICES.id_chosen_service = SERVICES.id_service
WHERE id_reservation = ?
NB: please note that column id_reservation in the WHERE clause is not prefixedeither. While this will not generate the same error (since that column only exists in one of the two tables), it is a good practice to always prefix columns when several tables are involved. Another good habit is to use table aliases.
Accordingly here is a new version of your query:
SELECT cs.id_service
FROM chosen_services cs
LEFT JOIN services s ON cs.id_chosen_service = s.id_service
WHERE cs.id_reservation = ?
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.
My sql query is to return title,date from one table and image name from another table using join based on Ad ID.
Here is the same.This works perfectly and fetches all data title, date from table 1 and image name from table 2.
SELECT wordpresswp_awpcp_ads.ad_title,
wordpresswp_awpcp_ads.ad_id,wordpresswp_awpcp_ads.ad_postdate,
wordpresswp_awpcp_media.name FROM wordpresswp_awpcp_ads
LEFT OUTER JOIN wordpresswp_awpcp_media on wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
Now for alphabet wise query i.e display ads staring with A,B.... I have passed a query in URL and rewrote the query as
SELECT wordpresswp_awpcp_ads.ad_title, wordpresswp_awpcp_ads.ad_id,
wordpresswp_awpcp_ads.ad_postdate,wordpresswp_awpcp_media.name FROM wordpresswp_awpcp_ads
LEFT OUTER JOIN wordpresswp_awpcp_media on wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
WHERE (wordpresswp_awpcp_ads.ad_title LIKE '$directory%' )
$directory takes alphabet passed from URL. With this query I am able to fetch data from table 1 i,e title name and date from not able to get the data from joined table i.e image name from table 2.
Any help would be appreciated.
Try this
SELECT wordpresswp_awpcp_ads.ad_title, wordpresswp_awpcp_ads.ad_id,
wordpresswp_awpcp_ads.ad_postdate,wordpresswp_awpcp_media.name
FROM wordpresswp_awpcp_ads
INNER JOIN wordpresswp_awpcp_media
ON wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
WHERE (wordpresswp_awpcp_ads.ad_title LIKE '$directory%' )
Your question is a bit unclear, but I guess you got into the classics.
When you add a where condition about your left joined table, then you get only rows which have that left join. So make sure you do not create a where-condition about wordpresswp_awpcp_media.
I think php variable is in single comm (') that's the problem please try
SELECT wordpresswp_awpcp_ads.ad_title, wordpresswp_awpcp_ads.ad_id,
wordpresswp_awpcp_ads.ad_postdate,wordpresswp_awpcp_media.name FROM wordpresswp_awpcp_ads
LEFT OUTER JOIN wordpresswp_awpcp_media on wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
WHERE (wordpresswp_awpcp_ads.ad_title LIKE "$directory%" )
Have these tables
and given post_map.tag_id='1', I would like to get:
entity_type table determines what table be look in, i.e. what table entity_id is stored in. My main goal is to get this table as the result of either mysqli::multi_query() or mysqli::query(), i.e. without PHP going back and forth to the database multiple times; this table may have many many rows and getting this table at once would much more efficient.
My attempts thus far:
I have tried JOIN clause but I don't know how to use the row value of prior SELECT as the table name for the JOIN clause.
Tried Prepared Statments but can't form anything usable.
It can be done by IF() and JOIN. I have solution for you, run below query...
SELECT et.type,
IF(et.type='resource',r.resource_type_id,NULL) AS resource_type_id,
IF(et.type='resource',r.value,NULL) AS value,
IF(et.type='user',u.name,NULL) AS name,
IF(et.type='link',l.source,NULL) AS source,
IF(et.type='link',l.count,NULL) AS count
FROM `post_map` as pm
JOIN `entity_type` as et ON pm.entity_id = et.id
LEFT JOIN `resource` as r ON pm.entity_type_id=r.id
LEFT JOIN `user` as u ON pm.entity_type_id=u.id
LEFT JOIN `link` as l ON pm.entity_type_id=l.id
WHERE pm.tag_id='1'
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