MYSQLI SQL query over multiple tables fail - mysql

I am very new to SQL and i need to retreive a number of attributes from different tables but. I am searching for images and details about a user by searching for them under their name.
For example:
SELECT * FROM table1 WHERE name LIKE '%tim%' AND last_name '%smith%'
I need the following attributes in my result:
I will need all the details from table 1
account id and image from table 2
path from table 4
I have tried
SELECT table1.id table1.name table1.last_name table 2.account_id table4.path FROM table1 table 2 table4 WHERE table4.image_id = table 2.image AND table1.first_name LIKE '%tim%' AND table1.last_name LIKE '%smith%'
I am considering spilting this up into multiple queries which would solve this issue, however I am sure this can be done under a single query?, I am just not sure how to structre it, I have tried a number of times, but all queries fail in phpmyadmin
Hope someone can point me in the right direction? Below are an example of my tables, thanks in advance!
table1
------------
id (pk)
name
last_name
table 2
--------------
account id (pk)
id (fk)
image (holds image id from table 4)
table 3
-----------------
account id (fk)
image id (fk)
date
table 4
--------------------
image id (pk)
path
date

Try with this:
SELECT t1.*, t2.account_id, t4.path
FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table4 t4 ON t4.image_id = t2.image

So you have INNER JOINs between table1 & table2, and between table2 & table4. In MySQL, JOIN implicitly means INNER JOIN.
SELECT
table1.id,
table1.name,
table1.last_name,
table2.account_id,
table4.path
FROM
table1
JOIN table2 ON table1.id = table2.id
JOIN table4 ON table2.image = table4.image_id
WHERE table1.name LIKE '%tim%' AND table1.last_name LIKE '%smith%'
(Edit: Forgot to add in the original WHERE clause.)

Related

How do I search relations on tables SQL

I have 3 tables like this
With the tables filled like this:
How do I search the cases In where on the table 3 the idtable 1 has all the id from table 2 related?
For example idtable1 = 1 would be an output of that query cuz is related with every id from idtable2
Presumably, you intend:
select t1.*
from table1 t1
where (select count(*) from table3 t3 where t3.idtable1 = t1.idtable1) =
(select count(*) from table2);
This shows all records from table1 where table3 contains all values of idtable2 -- assuming no duplicates in table3 (and that the ids are unique).

How do I include table names in a MySQL query?

This seems like it would be a simple setting, but I cannot find it. I do inner join queries on tables that have similar column names. It would be nice to include the table name in the query results, so the people receiving the data can differentiate more easily. For example:
Table1:
id
name
timestamp
Table2:
id
name
timestamp
table1_id
Table3:
id
name
timestamp
table2_id
Then I tie it all together with a query:
select * from table1
inner join table2 on table1.id=table2.table1_id
inner join on table2.id=table3.table2_id;
The results have similar column header names:
id name timestamp id name timestamp table1_id id name timestamp table2_id
It's hard to tell the data apart. Of course the example query is short and silly and pointless. If I do an actual query with all the data it get more complicated. Couldn't the column header name include the table name?
table1.id table1.name table1.timestamp table2.id table2.name table2.timestamp table2.table1_id table3.id table3.name table3.timestamp table3.table2_id
You have ambiguous column names in output: table1.id, table2.id
Adding alias for columns should solve this:
SELECT table1.id as t1_id, table2.id as t2_id
Instead of writing
select * from
you can write
select table1.id as table1_id,
and do the same for the other columns so that the results set would show you the names you give yourself for each column
You can use aliases to identify columns:
SELECT table1.id AS table1_id FROM ...
But you would have to do this for each field you want to select.
try this.hope it will help you.
SELECT table1.id as t1_id, table2.table1_id as t2_id
FROM tablename
inner join table2 on table1.id=table2.table1_id
inner join table3 on table2.id=table3.table2_id;

Mysql merge w tables with common column

I have:
table1:
Id | Name
table2:
Id | Amount
I want create a new table based on the common Id. So if a record from table1 and table2 have matching id, then:
table3
Id | name | Amount
Sorry if this has been asked before. I'm new to this and just want to get this done
Why wouldn't you do this with a simple select statement?
SELECT a.id, a.Name, b.Amount
FROM table1 a, table2 b
WHERE a.id = b.id
Try using a JOIN
SELECT table1.id, table1.name, table2.amount
FROM table1
LEFT JOIN table2
ON table1.id=table2.id;
I didn't test this but I think it should work.
http://www.w3schools.com/sql/sql_join_left.asp
Like this :
CREATE TABLE tavle_xxx(
id xxx,
name xxx,
amount xxx
);
INSERT IGNORE INTO tavle_xxx
SELECT t1.id, t1.name, t2.Amount
FROM table1 t1, table2 t2
WHERE t1.id=t2.id;
update:
I'd like to merge the tables based on the common column "id" and NOT create a 3rd table. Just add the "name column" and populate it where the id's are =
so i have:
table1:
Id | Name
table2:
Id | Amount
I would like the result to be
table2
Id | Amount | Name
If you want to merge the Name column of table1 in table2, using the common column Id, you can do this by running:
ALTER TABLE table1 ADD Name <dataTypeOfName>;
UPDATE table1, table2 SET table2.Name = table1.Name WHERE table1.Id = table2.Id;
This will create a new column called Name in table2 and then fill it with the values from table1, after performing a join based on the column Id.

SELECT * FROM table1 LEFT JOIN table1.value AS table2

i have a db like this:
Table1:
id
id_item
table (enum: 'table2','table3','table4')
table2:
id
value
table3:
id
value
table4:
[...]
And i want to run a query like this:
SELECT t1.id, t2.value FROM table1 AS t1
LEFT JOIN table1.table as t2 ON t1.id_item=t2.id
Is it possible? or i have to select first table1 and after the value?
( sorry for my bad eng :) )
If I understood the last column in Table 1 correctly and it is just a string, you can't.
You cannot write a column into the FORM-clausal and wait for mysql to evaluate it for every row and find the correct table to join it with.
To do this you will need to create a view where you will have the data from all the tables, together with the table name as an additional column. Afterwards you can perform a join like that between Table1 and the new view

join and select operation on two tables

i got two tables like
table1
=======
id primary
sku
price
vendor
table2
=====
id primary
sku
alt_sku
basically, my search term will be sku or alt_sku, and based on it, I want to select data from table1
what will be best approach to do this? should I create a third table to store relation between table1.sku and table2.sku /table2.alt_sku?? or how can I get data from table1
sorry, if this question is veryy foolish...I am trying to learn mysql, so for, I was on WP and never went to basics
You can try an inner join. Assuming TERM is the sku you are searching for.
SELECT
t1.*
FROM
table1 t1
inner join table2 t2 on (t1.sku = t2.sku)
WHERE
t1.sku = 'TERM'
or t2.alt_sku = 'TERM'
What exactly are you trying to do with Table2? Perhaps a better approach would be the following:
Table1
id
price
vendor
Table2
Table1Id
Sku
That way you can store a 1-N number of SKUs associated with Table1. If you only want 2 potential SKUs, then you could add a SkuType field to your Table2 - 1 for Primary and 2 for Alternate, but that may be going too deep for your needs...
And to query:
SELECT T.*
FROM Table1 T
JOIN Table2 T2 ON T.Id = T2.Table1Id
WHERE T2.Sku = 'SomeSku'
I think your table structure should be like that:
table1
=======
id primary
IdFromtable2
price
vendor
table2
=====
id primary
sku
alt_sku
Your Select Query will be like:
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.IdFromtable2 = t2.Id
WHERE t2.sku = #sku AND
t2.alt_sku = #alt_sku
Basically,The finger rule for table relationships is as following:
If the relations is one-to-many then you should save the PK from the many table as additional column in the one table.
If the relations is many-to-many you should create a third table (connecting table). This table will have both PK from both tables (for each tbl1 PK you will have all tbl2 PK's and vice versa).
That way you preventing duplicated records.