Selecting multiple tables from database - php

I am trying to make my php code to read off of two tables in my database. Like if it does not exist in one table it will check the other and see.
$mystyle = mysql_query("SELECT * FROM images WHERE `name` = '$name'");
How would I make it read from the table images and the table images_2
I tried doing this: [but didn't work of course]
$mystyle = mysql_query("SELECT * FROM images, images_2 WHERE `name` = '$name'");

Use UNION(implicit distinct) or UNION ALL :
SELECT * FROM images WHERE `name` = '$name'
UNION ALL
SELECT * FROM images_2 WHERE `name` = '$name'
Assuming images and images_2 has the same table structure, otherwise you have to list the columns' names explicitly instead of SELECT *.
Note that: Use PDO instead of Mysql_* functions, it is deprecated and vulnerable to SQL Injection.

I think you will have to return a count of the rows from the SELECT on images and if zero then run the SELECT against images_2

You can use UNION ,
(SELECT 1 AS sort_col, col1a, col1b, ... FROM t1)
UNION
(SELECT 2, col2a, col2b, ... FROM t2) ORDER BY sort_col;
http://dev.mysql.com/doc/refman/5.0/en/union.html

Related

Combine SQL statement

I would like to select different column from different row from the same table with one statement . How will I combine this to be one?
SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'
You could just use each of two current queries as subqueries in one select statement:
SELECT
(SELECT shop_lat_log FROM customers WHERE phoneNumber = '254719401837') AS shop_lat_log,
(SELECT delivery_lat_log FROM customers WHERE phoneNumber = '25472054919') AS delivery_lat_log
FROM dual;
This assumes that each of your two queries returns a single value. If not, then perhaps a UNION would be more appropriate:
SELECT
shop_lat_log AS log_value,
'shop_lat_log' AS log_type
FROM customers
WHERE phoneNumber = '254719401837'
UNION ALL
SELECT
delivery_lat_log,
'delivery_lat_log'
FROM customers
WHERE phoneNumber = '25472054919'
You can use a UNION for that:
SELECT `shop_lat_log` FROM `customers` WHERE `phoneNumber`='254719401837'
UNION
SELECT `delivery_lat_log` FROM `customers` WHERE `phoneNumber`='25472054919'
Note that the second query must have the same number of columns as the first query, and the results will have the first query's column names.
So even though you're selecting the delivery_lat_log column in your second query, the results will be in the shop_lat_log column if you're fetching an associative array.
Use an SQL case statement
select case c.phone_number
when '254719401837'
then c.shop_lat_log
when '25472054919'
then c.delivery_lat_log
end as field
from customer as c
where c.phone_number in ('254719401837', '25472054919')

Which Clause to use instead of IN Clause in MYSQL

My Question is
Which Clause use instead of IN Clause in MYSQL with PHP
Because IN Clause Limit 1024 character.
My Character limit exceed to 1024 character.
SELECT * FROM TblUser
WHERE Status != 'Deleted' AND UserId IN (0,10,11,12,13,14,15,22,45,114,144,155,156,167,211,439,440,441,443,445,450,455,456,457,458,459,1111,1154,1156,1165,1451,1541,11111,11112,11113,11114,11115,11116,11117,11118,11119,11656,15451,16561,17671,18781,33131,33311,33411,54511,111110,111111,111112,111113,111114,111115,111116,111117,111118,111119,111120,111121,111122,111123,111124,111125,111126,111127,111128,111129,111130,111131,111132,111133,111134,111135,111136,111137,111138,111139,111140,111141,111142,111143,111144,111145,111146,111147,111148,111149,111150,111151,111152,111153,111154,111155,111156,111157,111158,111159,111160,111161,111162,111163,111164,111165,111166,111167,111168,111169,111170,111171,111172,111173,111174,111175,111176,111177,111178,111179,111180,111181,111182,111183,111184,111185,111186,111187,111188,111189,111190,111191,111192,111193,111194,111195,111196,111197,111198,111199,1111100,11112101,11112102,11112103,11112104,11112105,11112106,11112107,11112108,11112109,11112110,11112111,11112112,11112113,11112114,11112115,11112116,11112117,11112118,11112119,11112120,11112121,11112122,11112123,11112124,11112125,11112126,11112127,11112128,11112129,11112130,11112131,11112132,11112133,11112134,11112135,11112136,11112137,11112138,11112139,11112140,11112141,11112142,11112143,11112144,11112145,11112146,11112147,11112148,11112149,11112150,11112151,11112152,11112153,11112154,11112155,11112156,11112157,11112158,11112159,11112160,11112161,11112162,11112163,11112164,11112165,11112166,11112167,11112168,11112169,11112170,11112171,11112172,11112173,11112174,11112175,11112176,11112177,11112178,11112179,11112180,11112181,11112182,11112183,11112184,11112185,11112186,11112187,11112188,11112189,11112190,11112191,11112192,11112193,11112194,11112195,11112196,11112197,11112198,11112199,11112200);
Please help Which Clause use instead of IN Clause in MYSQL with PHP?
Is the list of values that are used in the IN statement always the same, or is that list the result of some other query? If the latter is the case, you could use that query as a subquery:
SELECT * FROM tbluser
WHERE status != 'Deleted'
AND userid IN
( SELECT userid FROM sometable WHERE ... )
Yes you are limited within an IN statement, one thing you could do is create a temporary table which stores the values.
CREATE TEMPORARY TABLE IF NOT EXISTS temp AS (SELECT userid FROM tbluser);
In PHP create your INSERT INTO script:
$str = '0,10,11,12,13,14,15,22,45,114,144,155,156,167,211,439,440,441,443,445,450,455,456,457,458,459,1111,1154,1156,1165,1451,1541,11111,11112,11113,11114,11115,11116,11117,11118,11119,11656,15451,16561,17671,18781,33131,33311,33411,54511,111110,111111,111112,111113,111114,111115,111116,111117,111118,111119,111120,111121,111122,111123,111124,111125,111126,111127,111128,111129,111130,111131,111132,111133,111134,111135,111136,111137,111138,111139,111140,111141,111142,111143,111144,111145,111146,111147,111148,111149,111150,111151,111152,111153,111154,111155,111156,111157,111158,111159,111160,111161,111162,111163,111164,111165,111166,111167,111168,111169,111170,111171,111172,111173,111174,111175,111176,111177,111178,111179,111180,111181,111182,111183,111184,111185,111186,111187,111188,111189,111190,111191,111192,111193,111194,111195,111196,111197,111198,111199,1111100,11112101,11112102,11112103,11112104,11112105,11112106,11112107,11112108,11112109,11112110,11112111,11112112,11112113,11112114,11112115,11112116,11112117,11112118,11112119,11112120,11112121,11112122,11112123,11112124,11112125,11112126,11112127,11112128,11112129,11112130,11112131,11112132,11112133,11112134,11112135,11112136,11112137,11112138,11112139,11112140,11112141,11112142,11112143,11112144,11112145,11112146,11112147,11112148,11112149,11112150,11112151,11112152,11112153,11112154,11112155,11112156,11112157,11112158,11112159,11112160,11112161,11112162,11112163,11112164,11112165,11112166,11112167,11112168,11112169,11112170,11112171,11112172,11112173,11112174,11112175,11112176,11112177,11112178,11112179,11112180,11112181,11112182,11112183,11112184,11112185,11112186,11112187,11112188,11112189,11112190,11112191,11112192,11112193,11112194,11112195,11112196,11112197,11112198,11112199,11112200';
$ids = explode(',', $str);
foreach ($ids as $value){
echo 'INSERT INTO temp VALUES(' . $value . '); </br>';
}
But change the echo for the mysqli query.
Then:
SELECT * FROM tbluser u
where status !='Deleted'
And exists(select * from temp u1 where u1.userid = u.userid)
Or you can do an inner join temp u1 on u1.userid = u.userid
#Abid as you mentioned your question in this case #Guss is right.
What i am thinking if values mentioned IN clause comes from GROUP_CONCAT then might be possible duplicate values exist with comma seperated. So to remove duplicacy we can use DISTINCT.
For example :
SELECT GROUP_CONCAT(UserId) FROM sometable
Above query can give duplicate values.
SELECT GROUP_CONCAT(DISTINCT UserId) FROM sometable
SELECT t1.UserRoleId, t1.EntityId, GROUP_CONCAT( DISTINCT t1.PermissionId ) AS Permissions
FROM userpermission t1, bill_companies t2
WHERE t1.Status = 'Active'
AND t2.status = 'Active'
AND UserGroupId
IN ( 84, 85, 86 )
LIMIT 0 , 30
Above query gives you userid's without duplicacy. This can also reduce the size of comma seperated ids.
Hope this will help.
You could create groups using php if there are Ids which follow by increment e. g. 1,2,3 and replace them by a OR (id >= 1 AND id <= 3)
Alternative approach:
Search Max and Min ID in your List and query using an interval (greater than and less than) and use php to do the job.
If you really want to avoid using IN then you could use FIND_IN_SET():-
SELECT * FROM TblUser
WHERE Status != 'Deleted' AND FIND_IN_SET(UserId, '0,10,11,12,13,14,15,22,45,114,144,155,156,167,211,439,440,441,443,445,450,455,456,457,458,459,1111,1154,1156,1165,1451,1541,11111,11112,11113,11114,11115,11116,11117,11118,11119,11656,15451,16561,17671,18781,33131,33311,33411,54511,111110,111111,111112,111113,111114,111115,111116,111117,111118,111119,111120,111121,111122,111123,111124,111125,111126,111127,111128,111129,111130,111131,111132,111133,111134,111135,111136,111137,111138,111139,111140,111141,111142,111143,111144,111145,111146,111147,111148,111149,111150,111151,111152,111153,111154,111155,111156,111157,111158,111159,111160,111161,111162,111163,111164,111165,111166,111167,111168,111169,111170,111171,111172,111173,111174,111175,111176,111177,111178,111179,111180,111181,111182,111183,111184,111185,111186,111187,111188,111189,111190,111191,111192,111193,111194,111195,111196,111197,111198,111199,1111100,11112101,11112102,11112103,11112104,11112105,11112106,11112107,11112108,11112109,11112110,11112111,11112112,11112113,11112114,11112115,11112116,11112117,11112118,11112119,11112120,11112121,11112122,11112123,11112124,11112125,11112126,11112127,11112128,11112129,11112130,11112131,11112132,11112133,11112134,11112135,11112136,11112137,11112138,11112139,11112140,11112141,11112142,11112143,11112144,11112145,11112146,11112147,11112148,11112149,11112150,11112151,11112152,11112153,11112154,11112155,11112156,11112157,11112158,11112159,11112160,11112161,11112162,11112163,11112164,11112165,11112166,11112167,11112168,11112169,11112170,11112171,11112172,11112173,11112174,11112175,11112176,11112177,11112178,11112179,11112180,11112181,11112182,11112183,11112184,11112185,11112186,11112187,11112188,11112189,11112190,11112191,11112192,11112193,11112194,11112195,11112196,11112197,11112198,11112199,11112200')
However the only limit for the number of entries in an IN clause is set by max_allowed_packet (as others have stated) and the same restriction would apply to this.
max_allowed_packet

remove identical values from 2 mysqli queries

I want to make 2 mysqli queries. But if there are values in the second query that are Identical to the values yielded from the first query I want to exclude those values from the result set. What I have now only seems to work for 1 identical value. the rest of the identical values are shown. how should I change this? Thanks.
$query1 = $db->query("SELECT colTab1 FROM table1");
while ($result1 = $query1 ->fetch_assoc()) {
$query2 = $db->query("SELECT colTab2 FROM table2 WHERE colTab2 <> $result1[colTab1]");
echo $result1['colTab1']."<br>";
}
while ($result2 = $query2 ->fetch_assoc()) {
echo $result2['colTab2']."<br>";
}
}
Well, you can modify your second query as follows:
SELECT colTab2 FROM table2 WHERE colTab2 NOT IN (SELECT colTab1 FROM table1)
Or maybe you just want to select the UNION of the two tables (which will omit duplicates by default):
SELECT colTab1 FROM table1
UNION
SELECT colTab2 FROM table2
(Note that relying on UNION to omit duplicates between the two recordsets is not quite the same thing, as any duplicates that exist within each recordset will also be omitted; if that is a concern, one can SELECT DISTINCT ... UNION ALL SELECT DISTINCT ... instead).
Just run one query; why bring back data from the database to filter on? Filter in the DB.
SELECT colTab2
FROM table2
WHERE colTab2 NOT IN (
SELECT colTab1
FROM table1
)

MySQL query record with max value in a specific column

I know that this must be a very basic question, but I've not found an answer.
As the title says, I would like the query the record that holds the max value of a specific column.
I use the following code to achieve that:
SELECT * FROM `table_name` ORDER BY `column_with_specific_max_value` DESC LIMIT 1
I would like to know if there is an other way to achieve the same result (more parsimonious)? I know that SQL has a function MAX(column) but it's not working the way I want. I tried this:
SELECT * FROM `table_name` WHERE `column_with_specific_max_value`=MAX(`column_with_specific_max_value`)
and this:
SELECT *, MAX(`column_with_specific_max_value`) FROM `table_name`
What happen if the column_with_specific_max_value has 2 rows with the same max value? will it return both rows?
What about?
select * from table1
where score in (select max(score) from table1)
Or even without a max:
select * from table1
where score >= all (select score from table1)
Any of those WILL return all rows with the max value. You can play with it here.
If your table has an auto-increment column to work with, you could do something like...
select
YT3.*
from
( select
MAX( YT2.AutoIncColumn ) as ReturnThisRecordID
from
( select max( YT1.WhatColumn ) as MaxColumnYouWant
from YourTable YT1 ) JustMax
Join YourTable YT2
on JustMax.MaxColumnYouWant = YT2.WhatColumn ) FinalRecord
JOIN YourTable YT3
on FinalRecord.ReturnThisRecordID = YT3.AutoIncColumn
I would also ensure this is a column that SHOULD have an index on it, otherwise, you'll always be doing a table scan.

ORDER BY RAND() returns duplicates

I tried using distinct as well and it returns duplicates.
$cubes = mysql_query("SELECT distinct * FROM posts ORDER BY RAND() $limit ") or die(mysql_error());
I just want to take my posts table... and return it in a random order without duplicates.
Select only the distinct id's you need, e.g.
SELECT distinct id FROM posts ORDER BY RAND() $limit
Distinct works over 'all' rows you select, so if you (for example) have a unique timestamp field, chances are you'll return every single row.
Are you sure that you want to execute a SELECT DISTINCT * FROM ... and not just a SELECT DISTINCT column_name FROM ... ?
See the SQL DISTINCT STATEMENT doc for more infos.

Categories