MySQL join two completely identical tables with WHERE - mysql

I have two of same tables with different names. The first one's name is "folders" and the second one is "folders_archive".
I like to INNER JOIN this two tables with thier full content by filtering it on a date. If I use two single query's, thats works fine, but the joined query has no result.
I like something like this:
SELECT *
FROM folders
INNER JOIN folders_archiv
ON folders_archiv.id= folders.id
WHERE folders_archiv.datum = '".$year."-".$month."-".$day."'
AND folders.datum = '".$year."-".$month."-".$day."';
I'm trying to connect these tables in several way, but something is wrong with my logic, please help to fix it.
Thanks.

You question is not very clear, by i think you need Union - gives you all records from two table without repeat(if repeat is acceptable use union all). For example:
select t1.* from (
SELECT column_names
FROM folders
UNION
SELECT column_names
FROM folders_archive) t1
WHERE t1.column_names = what_you_want;

Very vague question.
But try this:
SELECT *
FROM folders
INNER JOIN folders_archiv
ON folders_archiv.datum= folders.datum
WHERE folders_archiv.datum = '".$year."-".$month."-".$day."'

Related

comparing two table columns in mysql results in duplicate

I have two tables, I've been trying to print the result from each but they are being duplicated. These are the two MySQL tables and the result. Notice the duplication.
The sql code for the project is:
SELECT * FROM savings,savtype WHERE cust_id=".$_SESSION['user']
I'm also looking for a work around this, in the meantime, id appreciate any assistance on this.
because you are not specifying how the two tables are related. You need to add that, either via an explicit ... JOIN ... (USING|ON)
SELECT
*
FROM
savings JOIN savtype USING (savtype_id)
WHERE
cust_id = ".$_SESSION['user']
or by providing the criteria in the where clause.
SELECT
*
FROM
savings, savtype
WHERE
savings.savtype_id = savtype.savtype_id AND
cust_id = ".$_SESSION['user']
As I understand from the screenshot you added, it makes joint between those tables, and what you probably want it left join from savings and savtype tables.
SELECT *
FROM `savings`
LEFT JOIN `savtype`
ON savings.savtype_id=savtype.savtype_id
where cust_id=".$_SESSION['user'] .";
Update if this did the trick,
You can learn more about left join here: https://www.w3schools.com/sql/sql_join_left.asp

MySQL multiple select with different wheres in single query

First of, at the moment I am forced to use MySQL despite it being deprecated. I am very well aware of that fact. Hopefully you guys can still help me with my sql syntax.
I am trying to access several columns from some different tables. Problem is, some of the columns require a different where clause than the last column I need, and the where clause for the last column requires info from the rest of the query so I cannot split it up into multiple queries, I've tried.
I cannot use union because one select statement selects four columns, and the other one selects one column.
Query:
(SELECT DISTINCT inventory.Quantity, itemtypes.Itemtypename,
itemtypes.ItemtypeID, inventory.ItemID
FROM inventory JOIN itemtypes ON inventory.ItemtypeID = itemtypes.ItemtypeID
JOIN sets ON inventory.SetID = sets.SetID
WHERE inventory.ItemtypeID = itemtypes.ItemtypeID
AND itemtypes.Itemtypename = 'Set'
AND sets.SetID = '".$setid."')
UNION
(SELECT DISTINCT sets.Setname
FROM sets JOIN inventory ON sets.SetID = inventory.ItemID
WHERE sets.SetID = inventory.ItemID)
This is what I tried to use, with no success. I cannot seem to find any other way of linking to different select statements without using multiple queries (with due to the structure of my PHP file, is impossible to do properly). The rest of the file will work, if only this problem gets solved. Hopefully you guys can help me with this, please let me know if this is even possible to do. Let me know if you need to see my PHP code as well. $setid is derived from a get in the file and contains a value existing in the database.
May be, you can try like this. It works fine in SQL Server.
SELECT TMP1.Quantity,TMP1.Itemtypename, TMP1.ItemtypeID, TMP1.ItemID, TMP2.Setname
FROM(
SELECT DISTINCT
inventory.Quantity,
itemtypes.Itemtypename,
itemtypes.ItemtypeID,
inventory.ItemID
FROM inventory
JOIN itemtypes ON inventory.ItemtypeID = itemtypes.ItemtypeID
JOIN sets ON inventory.SetID = sets.SetID
WHERE inventory.ItemtypeID = itemtypes.ItemtypeID
AND itemtypes.Itemtypename = 'Set'
AND sets.SetID = '".$setid."'
) AS TMP1,
(
SELECT DISTINCT
sets.Setname
FROM sets
JOIN inventory ON sets.SetID = inventory.ItemID
WHERE sets.SetID = inventory.ItemID
) AS TMP2
Hope this is what you want. Your question seems very ambiguous.

MySql Multiple Tables query

I wonder if any can help me understand something I'm trying to solve.
I'm working on a wordpress site but this is more a sql question as I'm just querying to get some results within a template file.
I have a gallery of pictures which are advert boxes, and I need to pull these in relation to a supplied movie name, to do this Im using some custom fields on the ad pic called 'adlink' (link off ad) and ad
I'm using the nextgen gallery plugin and querying those tables, and I have three tables in total that contain the data I need to query.
ngg_pictures, nggcf_field_values & nggcf_fields.
the nggcf tables are custom fields tables,
I have got so far I can get what I need in two seperate queries, but I can't combine these into one query as it means querying the nggcf_field_values table twice, which I can't seem to sort.
I have hardcoded the search criteria in for the mo, but the 'close-encounters' bit would be a passed var, and the '156' would be the pid from the first query.
SELECT `eg_ngg_pictures`.`filename`, `eg_nggcf_field_values`.`fid`, `eg_nggcf_field_values`.`pid`
FROM eg_ngg_pictures, eg_nggcf_field_values
WHERE ((`eg_nggcf_field_values`.`field_value` LIKE 'close-encounters') AND (`eg_nggcf_field_values`.`pid` = eg_ngg_pictures.pid))
SELECT `eg_nggcf_field_values`.`field_value`
FROM eg_nggcf_field_values, eg_nggcf_fields
WHERE ((`eg_nggcf_fields`.`field_name` = 'adlink') AND (`eg_nggcf_fields`.`id` = eg_nggcf_field_values.fid) AND (`eg_nggcf_field_values`.`pid` = '156'))
any help would be greatly appreciated, I can get the results with what I have, but I like to understand how to combine these two and write better SQl. Thanks MRO
After looking at the Wordpress extension, I think the eg_nggcf_fields is the table that contains the name for a custom field. The eg_nggcf_field_values table contains the values of that custom field per picture.
So if you're looking for two fields called moviename and adlink, you have to look up two rows in the field_values table. You can join a table twice if you give it a different alias:
select pic.filename
, pic.pid
, fv1.field_value as MovieName
, fv2.field_value as Adlink
from eg_ngg_pictures pic
inner join -- Find ID for the field called 'moviename'
eg_nggcf_fields f1
on f1.field_name = 'moviename'
inner join -- Find value for field moviename for this picture
eg_nggcf_field_values as fv1
on fv1.pid = pic.pid
and fv1.fid = f1.fid
inner join -- Find ID for the field called 'adlink'
eg_nggcf_fields f2
on f2.field_name = 'adlink'
inner join -- Find value for field adlink for this picture
eg_nggcf_field_values as fv2
on fv2.pid = pic.pid
and fv2.fid = f2.fid
where fv1.field_value like 'close-encounters'
First of all, I'd recommend sticking to modern ANSI syntax for JOINing tables, which means using the JOIN clause.
Instead of using:
FROM table1, table2 WHERE table1.id = table2.pid
use:
FROM Table 1 JOIN table2 ON table1.id = table2.id
For simplicity's sake, I'd also recommend you to alias tables, as that tends to make the code more readable. Instead of having to write out egg_ngg_pictures every time, you can simply refer to the alias you assign it instead.
Lastly, when you use a LIKE operator, you usually add a wild-card character (typically %. I.e. LIKE '%123' or LIKE '123%'). You seem to look only for complete matches, which means you can just stick to using =, as that should give you slightly better performance.
Now to rewrite your query, I'd use something like the following:
SELECT
pic.filename
, fieldval.fid
, fieldval.pid
, fieldval.field_value
FROM
eg_ngg_pictures pic
JOIN eg_nggcf_field_values fieldval ON fieldval.pid = pic.pid
JOIN eg_nggcf_fields fields ON fields.id = fieldval.fid
WHERE
((fieldval.field_value = 'close-encounters')
AND fields.field_name = 'ad_link'
Note that I am not able to test the query, as I do not have your schema. But by incorporating the two queries into a single query, the join on the field_Values.PID retreieved with the 'close_encounters' value should already exist.
If the query does not work, feel free to create a SQL fiddle with the relevant tables and some data, and I'll try and get it to work with that.

Join several times

I've a table with fields id_osztaly, id_csoportositas and name (and some other fields but there aren't important).
I want create a query with the follow result: I want combine the name fields. I can't explain so I show an example:
the datas in the table (id_osztaly, id_csoportositas and name order):
1,1,Group1A
1,1,Group1B
1,2,Group2A
1,2,Group2B
I want the follow combine from name:
Group1A-Group2A
Group1A-Group2B
Group1B-Group2A
Group1B-Group2B
(similar the permutation). I can do this with a JOIN, it's ok. But when I three different value of id_csoportositas:
1,1,Group1A
1,1,Group1B
1,2,Group2A
1,2,Group2B
1,3,Group3A
1,3,Group3B
1,3,Group3C
I want:
Group1A-Group2A-Group3A
Group1A-Group2A-Group3B
Group1A-Group2A-Group3C
Group1A-Group2B-Group3A
Group1A-Group2B-Group3B
Group1A-Group2B-Group3C
Group1B-Group2A-Group3A
Group1B-Group2A-Group3B
Group1B-Group2A-Group3C
Group1B-Group2B-Group3A
Group1B-Group2B-Group3B
Group1B-Group2B-Group3C
Yes, it's double join. But I don't know how many different id_csoportositas exist in table. First blick I think I need same number of JOIN as the number of different id_csoportositas.
Is there any trick in (my)sql to do this or should I do it in PHP with a for-cycle?
EDIT maybe I wasn't clear. I know how can I JOIN same table two times or three times. If I've two different id_csoportositas I need only one JOIN. If I've three different id_csoportositas I need two JOIN - I can do this too. But I don't know how many different id_csoportositas exist so I don't know how many JOIN will need. The number of JOINs depends on number of different id_csoportositas and I don't know the number of id_csoportositas without a query.
And I want to group by id_osztaly and different id_osztaly has different id_csoportositas.
I hope it's clear now.
Won't something like this work:
SELECT * FROM
(
(SELECT * FROM table WHERE something = 1) a,
(SELECT * FROM table WHERE something = 2) b,
(SELECT * FROM table WHERE something = 3) c
)
You just select like this and it would provide all possible variations of the joins :)

Multiple tables in one view?

Today my question is how would I go about creating a view in a MySQL database that uses more than two tables?
Here is my query (it works) I am not looking to change my current query, mostly looking for a nice reference with examples on this topic.
CREATE OR REPLACE VIEW vw_itemsPurchased AS
SELECT `tbl_buyers`.`fldPrimaryKey` as fldFKeyBuyer, `tbl_buyers`.`fldEmail` as fldBuyerEmail, `tbl_buyers`.`fldAddressStreet`, `tbl_buyers`.`fldAddressCity`, `tbl_buyers`.`fldAddressState`, `tbl_buyers`.`fldAddressZip`, `tbl_buyers`.`fldAddressCountry`, `fldPaymentCurrency`, `fldPaymentGross`, `fldPaymentStatus`, `fldReceiverEmail`, `fldTransactionId`
FROM `tbl_transactions` INNER JOIN `tbl_buyers`
ON `tbl_transactions`.`fldFKeyBuyer` = `tbl_buyers`.`fldPrimaryKey`
Thanks for your time!
To use more than two tables, you simply continue adding JOIN statements to connect foreign keys. Adapting your code to add an imaginary third table tbl_products might look like this:
CREATE OR REPLACE VIEW vw_itemsPurchased AS (
SELECT
tbl_buyers.fldPrimaryKey as fldFKeyBuyer,
tbl_buyers.fldEmail as fldBuyerEmail,
tbl_buyers.fldAddressStreet,
tbl_buyers.fldAddressCity,
tbl_buyers.fldAddressState,
tbl_buyers.fldAddressZip,
tbl_buyers.fldAddressCountry,
fldPaymentCurrency, fldPaymentGross,
fldPaymentStatus,
fldReceiverEmail,
fldTransactionId,
tbl_tproducts.prodid
FROM
tbl_transactions
INNER JOIN tbl_buyers ON tbl_transactions.fldFKeyBuyer = tbl_buyers.fldP
-- Just add more JOINs like the first one..
JOIN tbl_products ON tbl_products.prodid = tbl_transactions.prodid
In the above method, the first and second tables relate, and the first and third tables relate. If you have to relate table1->table2 and table2->table3, list multiple tables in the FROM and relate them in the WHERE. The below is just for illustration and doesn't make much sense, as you probably wouldn't have a customer id in the same table as a product price.
SELECT
t1.productid,
t2.price,
t3.custid
FROM t1, t2, t3
WHERE
-- Relationships are defined here...
t1.productid = t2.productid
AND t2.custid = t3.custid