php select from where multi dual - php

I want to insert some data into database. During the insert, I want to make a judge first: if title not exist in table1 and table2, then insert. how to write this dual sentence? Is this right? OR... Thanks.
mysql_query("
INSERT INTO table1 title
SELECT '".$title."'
FROM dual
WHERE not exists (
SELECT title
FROM table1
WHERE table1.title = '".$title."'
) AND (
SELECT title
FROM table2
WHERE table2.title = '".$title."'
) ");

First of all, MySQL does not use the dual table. Just select the value directly. Normally to insert into a table, you only need to use either
INSERT INTO table1(title) SELECT 'something'; # or
INSERT INTO table1(title) VALUES ('something');
However, the below uses a made up table (aliased TT) to be able to use a LEFT JOIN on it to other tables.
mysql_query("
INSERT INTO table1(title)
SELECT theTitle
FROM (SELECT '".$title."' theTitle) TT
LEFT JOIN table1 ON table1.title = TT.theTitle
LEFT JOIN table2 ON table2.title = TT.theTitle
WHERE table1.title is null and table2.title is null
");
You can also just complete your NOT EXISTS clauses
mysql_query("
INSERT INTO table1(title)
SELECT '".$title."'
WHERE NOT EXISTS (SELECT * FROM table1 WHERE title = '".$title."')
AND NOT EXISTS (SELECT * FROM table2 WHERE title = '".$title."')
");
But I prefer the former, because this requires you to use $title 3 times.

mysql_query("INSERT INTO table1 title
SELECT '".$title."' FROM dual
WHERE not exists (SELECT title FROM table1 WHERE table1.title = '".$title."') AND
not exists (SELECT title FROM table2 WHERE table2.title = '".$title."') ");
missing second not exists in query.

Union anyone?
mysql_query("
INSERT INTO table1 title
SELECT '".addSlashes($title)."'
FROM dual
WHERE not exists (
SELECT title
FROM table1
WHERE title = '".addSlashes($title)."'
UNION
SELECT title
FROM table2
WHERE title = '".addSlashes($title)."'
) ");

Related

MYSQL unable to load OR operator on different tables

I think nothing wrong with my query, but don't know why system becomes very slow when I load this code in phpmyadmin. Any advice please help.
select
*
from
`table1`
left join
`table2`
on
table1.id = table2.id
where
(
(table1.email = 'test#test.com' and table1.mobile = '99999999')
or
(table2.email = 'test#test.com' and table2.mobile = '99999999')
)
Thanks in advance.
Add this to each table:
INDEX(email, mobile) -- in either order.
If that does not speed it up enough, then also turn the OR into a UNION:
SELECT *
FROM ( ( SELECT id FROM table1 WHERE email = ... AND mobile = ... )
UNION DISTINCT
( SELECT id FROM table2 WHERE email = ... AND mobile = ... )
) AS x
JOIN table1 WHERE table1.id = x.id
JOIN table2 WHERE table2.id = x.id
Add these indexes:
ALTER TABLE `table1` ADD INDEX `table1_idx_id` (`id`);
ALTER TABLE `table2` ADD INDEX `table2_idx_id` (`id`);
ALTER TABLE `table1` ADD INDEX `table1_idx_email_mobile_id` (`email`, `mobile`, `id`);
ALTER TABLE `table2` ADD INDEX `table2_idx_email_mobile_id` (`email`, `mobile`, `id`);
And the transformed query (avoiding the OR condition, which is not indexable, by using UNION DISTINCT):
(SELECT
*
FROM
`table1`
LEFT JOIN
`table2` ON table1.id = table2.id
WHERE
((table2.email = 'test#test.com'
AND table2.mobile = '99999999'))) UNION DISTINCT (SELECT
*
FROM
`table1`
LEFT JOIN
`table2` ON table1.id = table2.id
WHERE
((table1.email = 'test#test.com'
AND table1.mobile = '99999999')))
P.S, if there is no option to have duplicates, use UNION ALL instead of UNION DISTINCT, as it should perform better (avoid the duplicates elimination, which can take time).

Retrieve data from two tables having id and display the max id of another table in Mysql

I have a two different tables
Table 1
id
name
description
Table 2
id
details
info
table1_id
I want to display all the records from the table1 except id but from table2 I used to display the max id.
eg. table1 have following records
id=1
name = test
description = some text
table2 have
id=5
details = some more text
info = the new info
table1_id = 1
so the result what I want is
id name description
5 test some text
Try this:
select
(select max(table2.id) from table2 where table1.id = table2.table1_id) id,
name,
description
from table1
or left join:
select
t.id,
table1.name,
table1.description
from table1
left join (
select max(id) id, table1_id from table2 group by table1_id
) t on table1.id = t.table1_id
You can try with and max.
with ID_Table_1_MaxID_Table_2 as (
select table1_id, max(id) Max_Table2_ID
from Table_2
group by table1_id
)
SELECT tb2.id, tb1.name, tb1.description
FROM Table_2 tb2
INNER JOIN ID_Table_1_MaxID_Table_2 sub
ON (sub.table1_id = tb2.table1_id and tb2.id = sub.Max_Table2_ID)
INNER JOIN Table_1 tb1 on tb1.id = sub.table1_id

Regroup two SELECT

This query select all from my first table where the row id doesnt exist in my second table:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT idTable1 FROM table2 WHERE table1.id=idTable1
This query select all row of table1 who exists in the table2 with the last id of table2 ( cause one row of table1 can have multiple row of the table2 but only the last is not forget ):
SELECT *
FROM table2
INNER JOIN table 1 ON idTable1 = table1.id
WHERE table2.id IN (SELECT MAX(actioncur.id) FROM table2 GROUP BY idTable1)
I want to regroup the both query in one, I want to select all row of table1 when id doesnt exist in table2 and select all of the table1 for the last table2 id. For exemple i want to select that : row 1 -> id=44 ; table2.id= 187 ; idTable1=44. Row 2 ->id=45 ; table2.id=? ; idTable1=?
If I understand correctly, you can use a correlated subquery:
SELECT t1.*,
(SELECT MAX(t2.id)
FROM table2 t2
WHERE t1.id = t2.idTable1
) as max_t2id
FROM table1 t1;
You can also do this with LEFT JOIN and GROUP BY:
SELECT t1.*, MAX(t2.id) as max_t2id
FROM table1 t1 JOIN
table2 t2
ON t1.id = t2.idTable1
GROUP BY t1.id;

how to call sub query in if conditional statement on mysql query

I have two tables table1 and table2. i need value of table1 when status=1
SELECT type, no,
CASE WHEN status = '0' THEN 'Vacant'
WHEN status = '2' THEN 'Repair'
ELSE (SELECT `field1` FROM `table1` WHERE `field2`=10) AS test END AS addr
FROM table2
This query outputs error with #1064
how can I get the values in a single MYSQL query?
SELECT type, no,
CASE WHEN status = '0' THEN 'Vacant'
WHEN status = '2' THEN 'Repair'
WHEN status = '1' (SELECT `field1` FROM `table1` WHERE `field2`=10) END AS addr FROM table2
if you try this code then you won't get error
You can use a subquery:
SELECT * FROM t1
WHERE s11 > ANY
(SELECT COUNT(*) /* no hint */ FROM t2
WHERE NOT EXISTS
(SELECT * FROM t3
WHERE ROW(5*t2.s1,77)=
(SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
(SELECT * FROM t5) AS t5)));

PHP/SQL Statement repeating a query

If I have a list of ID's that I have selected from a statement
SELECT id FROM myTable WHERE name = 'TEST'
This would return just the ids (1001, 1002, 1003, etc...) Then I want to perform another SELECT statement to retrieve all the titles for all those ids.
SELECT title FROM myTable2 WHERE id = XXXX
the id in table2 is the foreign key of table2. id in myTable is the Primary Key. How can I go about retrieving all the titles from those ids. I was thinking about storing all the results of the first select statement in an array, and then using a while loop to iterate through the list and return each result into another array, but my fear is that when the database gets big if it has to return 1000 rows that could be some bad overhead. So in PHP or SQL what is the best way to perform this?
You can use a subquery:
SELECT title
FROM myTable2
WHERE id IN (
SELECT id
FROM myTable
WHERE name = 'TEST'
)
Another way to do it would to be use a JOIN, to avoid the sub-query:
SELECT title
FROM myTable2
LEFT JOIN myTable
ON myTable.id = myTable2.id
WHERE myTable.name = 'TEST'
You should just be able to select them at the same time.
SELECT a.id, b.title
FROM myTable a, myTable2 b
WHERE a.name = 'TEST' AND b.id = a.id;
to select both:
SELECT id, title FROM mytable WHERE name="TEST"
or to select the whole row
SELECT * FROM mytable WHERE name="TEST"
if its two tables you are selecting from:
SELECT id, title FROM mytable A JOIN mytable2 B USING (id)

Categories