Creating a table from a query in MySQL/PHP - php

I'm not sure this is possible, but the code I tried using first is:
mysql_query ("CREATE TABLE My_table as ( SELECT * FROM CPE LEFT JOIN Greenwich_j20 ON CPE.cust_index = Greenwich_j20.cust_index LEFT JOIN Liverpool_j20 ON CPE.cust_index = Liverpool_j20.cust_index)")
or die ("this certainly didn't work\n");
The query itself works fine, and the syntax for the table works fine, but the combination is what it really doesn't like. Does it have a problem creating a table from a left join query?

If there is any auto increment column in your existing tables that will be not remain auto_increment in the table to be created.
You could try by selecting columns instead of * there should be an error of column Ambiguous.

Related

Update Database Table Column From Another Table

I have two similar table in my database. I want update my database called qu_time in tbl_quotes from table new_quotes.
I have tried query like this:
UPDATE tbl_quotes
SET qu_time = (SELECT qu_time FROM new_quotes)
but I get an error
1242 - Subquery returns more than 1 row
Let me know if someone have idea to solve it.
Thanks
You should have atleast one common column in both tables.
Use that column in the join condition and do the updating..
UPDATE tbl_quotes t1
JOIN new_quotes t2
ON t1.Id_column = t2.Id_column
SET t1.qu_time= t2.qu_time

mysql select multiple tables and bring back all results

I am trying to select everything from multiple tables where there are 2 conditions.
But they only bring back a single result from the second table, instead of everything
Here is the MySql code
SELECT *
FROM `core_users`.`users`
LEFT JOIN `core`.`orders`
ON `core_users`.`users`.`uid` = `core`.`orders`.`uid`
LEFT JOIN `core_users`.`crm`
ON `core_users`.`users`.`uid` = `core_users`.`crm`.`uid`
WHERE (`core_users`.`users`.`signup_timestamp` BETWEEN '1476626400' AND '1476712800'
OR (`core_users`.`crm`.`type` = 'refresh_5in5' AND `core_users`.`crm`.`value` BETWEEN '1476626400' AND '1476712800') )
This just brings back 1 result from the crm table. However I want it to bring back all the results from the crm table.
How do I bring back everything from users, orders, and crm while having a WHERE clause on both tables?
Try select users.*, orders.*, crm.*
Please note that, in such case if you have common columns in any of these table like id in every table it would cause an ambiguous column name error. To get rid of that you need to specify these with alias name users.id as user_id, orders.id as order_id, crm.id as crm_id and so on.

SQL - Insert Into Select (multiple columns)

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)

Msql Inner Join Query Dilemma - Need Joined Auto Increment Value

I have two mysql tables that I am querying together and the query works fine. The tables are car and requests
The problem is that i need both the auto increment id's and it only gives me one.
The one it gives me is not the joined tabled.
Here is my query so far
SELECT * FROM `car` INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id` WHERE `car`.`user_id` =21
I just need to someway get the auto increment id of the requests table.
As always stack exchange is the best place to come for answers so thanks in advance!
Just specify your query as this and you'll get both but with different names:
SELECT `car`.id AS car_id, `requests`.id AS request_id, *
FROM `car`
INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id`
WHERE `car`.`user_id` =21
Note that you will still have an ID column which SHOULD be the requests.id, just diregard it and use car_id and request_id...
When you have to join tables and table contains same name than it creates an ambiguous situation. So always use table name or table alias with column name Like this
SELECT t.column
FROM table as t
OR
SELECT table.column
FROM table

Getting SQL result as nested array in PHP

I've a ('courses') table that has a HABTM relationship with ('instructors') table through another table...
I want to get the data of an instructor with all related courses in one query..
Currently, I have the following SQL:
SELECT *
FROM `instructors` AS `instructor`
LEFT JOIN `courses` AS `course`
ON `course`.`id` IN (
SELECT `course_id`
FROM `course_instructors`
WHERE `course_instructors`.`instructor_id` = `instructor`.`id`
)
WHERE `instructor`.`id` = 1
This SQL does what it should be doing, the only "problem" I have is that I get multiple rows for each joined rows.
My question is:
Can I get the result I want in one query? Or do I have to manipulate the data in PHP?
I'm using PHP and MySQL.
Each record of a query result set has the same format: same number of fields, same fields, same order of fields. You cannot change that.
SELECT *
FROM instructors AS instructor
LEFT JOIN
course_instructors
ON
instructor.id= course_instructors.instructor_id
LEFT JOIN
courses
ON
course_instructors.course_id = course.id
WHERE instructor.id = 1
This assumes the PK of course_instructors is (instructor_id,course_id)
Explanation of query:
First join + WHERE make sure you get the relevant instructor
Second join matches ALL the entries from the course_instructor table that belongs to this instructor. If none found, will return one row with NULL in all fields
Last join matches all relevant courses from the entries found from course_instructor If none would will return one record with NULL in all fields.
Again: important to use the right constraints to avoid duplicate data.
That's the nature of relational databases. You need to get the instructor first and then get the related courses. That's how I would do it and that's how I've been doing it. I'm not sure if there is a "hack" to it.

Categories